When you use a custom loop in WordPress, pagination may not work as expected. You may see only the first page of posts, regardless of which page you select. I often use WordPress as a content management system for client projects and encounter this problem with custom loops. Here is the solution:
Update query_posts with the proper query vars
The solution to this problem is pretty simple. You need to tell WordPress to use the "paged" query variable in your custom loop, otherwise it's ignored. If you're using the query_posts function for your custom loop, the easiest way to do this is like so:
1 2 | $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;query_posts('paged=' . $paged . '&cat=76'); // show posts in category 76 with pagination enabled |
In this example, we first test to see if the "paged" query variable is set using the get_query_var function, which will return false if it's not set. If it is set, we'll set $paged equal to the value of that query variable and if not, we'll set it equal to 1.
Then we initialize our custom loop using the query_posts function by setting "paged" equal to our $paged variable so that WordPress grabs the current page from the URL and displays the appropriate list of posts for that page.
I'm also looping through the posts in category #76 in this example, which is only there for illustrative purposes. That's all there is to enabling pagination in custom WordPress loops.




