Category Archive Navigation

A simple next/previous category navigation for the category archive; particular useful if you use your categories to organise your blog, for instance your products, or photos…

<?php
foreach(get_categories() as $all_cat) {  $cat_ids[] = $all_cat->term_id; }
 $this_cat = get_query_var('cat');
 $this_cat_position = array_search( $this_cat, $cat_ids ); ?>
<div>
<div>
<?php $prev_cat_position = $this_cat_position -1;
 if( $prev_cat_position >=0 ) {
 $prev_cat_id = array_slice( $cat_ids, $prev_cat_position, 1 );
 echo '<a href="' . get_category_link($prev_cat_id[0]) . '">&laquo; ' . get_category($prev_cat_id[0])->name . '</a>'; } ?>
</div>
<div>
<?php $next_cat_position = $this_cat_position +1;
 if( $next_cat_position < count($cat_ids) ) {
 $next_cat_id = array_slice( $cat_ids, $next_cat_position, 1 );
echo '<a href="' . get_category_link($next_cat_id[0]) . '">' . get_category($next_cat_id[0])->name . ' &raquo;</a>'; } ?>
</div>
</div>

the above has a commonly used html structure to help with formatting.

4 Replies to “Category Archive Navigation”

  1. This is great, helped me out very well. I have a special request of you if you have the time…

    I need something like the above code which will work for tags and months as well as categories. Can you do that?

Leave a Reply to Patrick Cancel reply

Your email address will not be published. Required fields are marked *