Categories Sub-menu
This is a very simple version of a Categories sub-menu implemented in the sidebar.php. Once a category is clicked, the sidebar should show the following:
- Category description (this is useful other than the sidebar)
- Children categories
- Posts in the category
Clicking on any category causes this menu to display all posts within that category. I generally try to stay away from children categories but children categories may also be displayed.
/* If this is a category archive
* This is a continuation of the if - elseif
* sequence from the default template sidebar.php
*/
<?php } elseif (is_category()) { ?>
<?php $current_category = single_cat_title(”, false); ?>
<?php $category_id = get_cat_ID($current_category); ?>
<li><h2>Description of <?php echo $current_category; ?></h2>
<p><?php echo category_description($category_id); ?></p>
</li>
<li><h2>Children of <?php echo $current_category; ?></h2>
<ul>
<?php wp_list_categories('orderby=name&show_count=1&hierarchical=0&title_li=&use_desc_for_title=0&child_of='. $category_id); ?>
</ul>
</li>
<li><h2>Posts in <?php echo $current_category; ?></h2>
<ul>
<?php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
</li>
<?php } ?>
What is going on here?
First we check to see if we are working with a category. If yes, we proceed. Next we assign the category name and the category ID to PHP variables. Then using some template tags and basic formatting, we output the category’s Description, Children, and Posts.






