Wordpress delivers a out-of-the-box pagination that is built into most themes. It allows the user to cycle through “previous” and “next” articles posted on the blog. But often, you may want to cycle through article posted just within that category, not the entire blog. This can easily be done with a small mod to the out-of-the-box pagination.
Here is the out-of-the-box code for pagination:
<div class="navigation">
<div class="alignleft"><?php previous_post_link('« %link') ?></div>
<div class="alignright"><?php next_post_link('%link »') ?></div>
</div>
If you want this to navigate through posts within the category of the current post simply change it to this:
<div class="navigation">
<div class="alignleft"><?php previous_post_link('« %link', '%title', 'true') ?></div>
<div class="alignright"><?php next_post_link('%link » ', '%title', 'true'); ?></div>
</div>
You can see that the “previous_post_link” and “next_post_link” stay there so the action is the same, but you can see that there are some added parameters here. Wordpress default usage of this function is this:
<?php previous_post_link('format', 'link', in_same_cat, 'excluded_categories '); ?>
As you can see, this allows you to set a boolean value of ‘true’ or ‘false’ for the in_same_cat. In our example, we just set that to true, and the pagination becomes centralized to the current category. You can also leave the boolean set to true, and use the excluded_categories to add a string value of categories to not be included in the pagination.