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…

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 ); ?>
<?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>'; } ?>
<?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>'; } ?>

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

Hierarchical Category List with Post Titles

If your wordpress site is organized with categories, it might be very useful to have a template to show an organized list of all categories with the associated posts.

This would work similar to the category widget with the setting ‘hierarchical’ – only that for each category, all the post titles are listed with links to the individual post.

This is the code:

* alchymyth 2011
* a hierarchical list of all categories, with linked post titles

 foreach( get_categories('hide_empty=0') as $cat ) :
 if( !$cat->parent ) {
 echo '<ul><li><strong>' . $cat->name . '</strong></li>';
 process_cat_tree( $cat->term_id );

 wp_reset_query(); //to reset all trouble done to the original query
function process_cat_tree( $cat ) {

 $args = array('category__in' => array( $cat ), 'numberposts' => -1);
 $cat_posts = get_posts( $args );

 if( $cat_posts ) :
 foreach( $cat_posts as $post ) :
 echo '<li>';
 echo '<a href="' . get_permalink( $post->ID ) . '">' . $post->post_title . '</a>';
 echo '</li>';

 $next = get_categories('hide_empty=0&parent=' . $cat);

 if( $next ) :
 foreach( $next as $cat ) :
 echo '<ul><li><strong>' . $cat->name . '</strong></li>';
 process_cat_tree( $cat->term_id );

 echo '</ul>';

The first step is to find all top level categories (generated in alphabetical order) – which is simply done with the wordpress function ‘get_categories()’ and by checking if the category has a parent.
Caveat: because the code is checking for top level categories only, you can’t directly use the parameters of ‘get_categories()’ (see the Codex, the mostly unread documentation of WordPress) without some customization of the top section of the code.

The second step is to show the titles of all available posts for each top level category, linked to the full individual post; done with ‘get_posts()’ and ‘get_permalink()’.

The third step is to find possible direct child categories for this (top) category; done with the ‘parent’ parameter of ‘get_categories()’.

Then step 2 and 3 are repeated until all possible categories are dealt with.

edit 11/06/2011: parameter corrected.

edit 10/02/2012: a slightly different code – without the post lists – for a nested hierarchical category list with links to the category archive and an edit link for the category: – initiated by this WPSE question

Easy Coding for WordPress: Posts in 3 Columns

Sometimes it is the simple things that are hard to find.

A basic structure to arrange posts into three columns:

<div id="column-wrap">
<?php $count = 0;

while(have_posts()) : the_post(); ?>

<div class="box<?php if( $count%3 == 0 ) { echo '-1'; }; $count++; ?>">

<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<!--and other output of the loop -->


<?php endwhile; ?>
</div><!--end column-wrap-->

The minimal styles needed for the three columns:

.box-1 { float:left; clear:left; width: 30%; margin-left: 0; }
.box { float:left; width: 30%; margin-left: 3%; }

  1. <div id=”column-wrap”>
  2. <?php
  3. $count = 0;
  4. while(have_posts()) : the_post()
  5. ?>
  6. <div class=”box<?php if( $count%3 == 0 ) { echo ‘-1’; }; $count++; ?>“>
  7. <a href=”<?php the_permalink(); ?>“>
  8. <h4><?php the_title(); ?></h4></a>
  9. <!–and other output of the loop –>
  10. </div>
  11. <?php endwhile; ?>
  12. </div><!–end column-wrap–>
  13. —————-
  15. .box-1 { float:left; clear:left; width: 30%; margin-left: 0; }
  16. .box { float:left; width: 30%; margin-left: 3%; }

Finding Exact Information on WordPress Functions

According to my motto:

‘Knowledge is Knowing where it is Written’
by Albert Einstein

– I like to look up the core files of WordPress where the functions are defined.

What I quite often do when I come across something new or not fully understood – a function, or template tag, or whatever wordpress related:

I search the web for it; not just with any keywords, but quite focussed.

Lets take the example of one of my earlier posts (Front Page Comments on your WordPress Blog) where I was investigating why comments don’t easily show on the front page or on archive pages of a blog:

The function in the template files that would show comments and the comment form, is ‘comments_template()’ – I therefore search for:

‘function comments_template()’

the search engine of my choice returns (apart from many other links) first of all the link to the documentation in the codex; and a link to the core file (and the exact line):

Obviously, my search engine knows me, so the relevant results show quite at the top of all possible search results; you may need to scroll down a few pages to find the important link: the one beginning with ‘PHPXRef’.

the link to follow is:

in the page that opens, you’ll get the line:
defined at: followed by a link.

click on that link;

that leads you to:

with a list of some functions; click on the title of the one you are looking up.

in there in line 851, you have it (the code that explains why comments do not automatically show on the front page; and what you can do to make them show):

851      if ( !(is_single() || is_page() || $withcomments) || empty($post) )

This search method is also great for finding related functions, global wordpress variables, and expanding your vocabulary and understanding of wordpress terms…