This is a useful little code snippet that enables you to show a list of subcategories and their descriptions within your template files. It’s most useful when creating custom wordpress templates for specific categories.
This code snippet is useful if you want to have a ‘master category page’ where you are using a top level category as a container, but don’t need to see a complete blog roll of the sub categories.
At the moment this is something that needs to be done at template level, but I plan to make this into a plugin with a WP tag that can be dropped into a post. For now, here’s how to include it in your template:
1. Take the code below and drop it into your theme’s functions.php file:
/*----------------------------------------*/
/* list categories and their descriptions */
/*----------------------------------------*/
function showSubCats($parentCat,$showParentDetails = 1,$moreText = 'more ⇒') {
$header = NULL;
$header .= "<ul>";
$footer = "</ul>";
$args = array(
'type' => 'post',
'child_of' => $parentCat,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'hierarchical' => 1,
'exclude' => NULL,
'include' => NULL,
'number' => NULL,
'pad_counts' => false );
//Defines an array of the sub categories
$subCats = get_categories($args);
$thisCat = get_category($parentCat);
//Test Output
// print "<pre>";
// print_r($args);
// print_r($thisCat);
// print_r($subCats);
// print "</pre>";
foreach($subCats as $key => $catData){
?>
<div>
<h2><?php print $catData->name; ?></h2>
<p><?php print $catData->description; ?><br />
<a href="/<?php print $thisCat->slug."/".$catData->slug; ?>"><? print $moreText; ?></a>
</p>
</div>
<?php
}
}
2. Call the function in your template file like this:
<?php showSubCats(parentcatid,'more link text'); ?>
Currently the output is in this form:
<div class="category-item"> <h2>Category Title</h2> <p>Category Description<br /> <a href="/slug-to-my-category">more text</a> </p> </div>
I’ve used this code in the ‘What I do’ page on this site in combination with Wessley Roche’s Category Order plugin. It’s important to note that if you do use Wessley’s plugin, you need to leave the orderby argument set as ‘name’ and the order as ‘ASC’.
I put no limitations on the use of this code, but if you use it, it would be nice if you gave a link back to this page, or at least let me know where you’ve used it.

