WP: Splitting Category Listing Into Multiple Pages
Jun 19th, 2008 | By dikiem | Category:WordPress Tips and Hack | no comments | 68 views |Here is a little neat trick for Wordpress. Normally, you would use the WordPress tag “query_posts” to query against the database to list all of the posts relating to one or more categories.
Usually a simple query against a category would like something like this:
<?php query_posts(showposts=5&cat=$category);
while(have_posts()) { the_post();
}
?>
Basically all this is doing is show 5 posts for the category, where $category is either one or more categories depending if you have a outter loop or not. What if you have a lot of posts per category and want to only show 5 per page. Plus you want to have the previous page and next page button. That would be cool huh?
You can easily do that. Using the above code as example, you can easily transform it to look like this.
<?php $paged = (get_query_var(’paged’)) ? get_query_var(’paged’) : 1; ?>
<?php query_posts(”cat=$category&posts_per_page=5&paged=$paged”);
while(have_posts()) { the_post();
}
?>
Somewhere else on that same page, you need to call the function code that handle the previous page and next page logic. They look something like this
<?php next_posts_link(’« Next Page’) ?> || <?php previous_posts_link(’Previous Page »’) ?>
What you’ve just done essentially is use the get_query_var to capture from the url string what page is it currently on and pass that to the page. At the bottom, it detect what page is next and previous.
You can see a sample in action from the SAPHELPBYKEVIN page link above.
Additional References:
Wordpress Query Post: http://codex.wordpress.org/Template_Tags/query_posts
no comments | 68 views
