I'm using the following wp_list_categories
code on category pages. If the current page is a parent category it displays all child categories of that category and if the current page is a child category it displays all the child categories of the parent category except the current category.
<?php if (is_category( )) {
$cat = get_query_var('cat');
$thiscat = get_category ($cat);
$parent = $thiscat->parent;
$catname = get_cat_name( $cat );
if ($parent != '') {
echo '<h2 class="widget-title"><span>Related Products</span></h2>';
echo '<ul>';
wp_list_categories( array(
'title_li' => '',
'child_of' => $parent,
'exclude' => $cat,
'use_desc_for_title' => 0
) );
echo '</ul>';
}
else {
echo '<h2 class="widget-title"><span>' . $catname . ' Products</span></h2>';
echo '<ul>';
wp_list_categories( array(
'title_li' => '',
'child_of' => $cat,
'use_desc_for_title' => 0
) );
echo '</ul>';
}
} ?>
However, I now realize that I need to wrap each of the listed categories in a <h3>
header element. Am I right in assuming that the only way to do this is to use get_categories
and then output the required HTML using a foreach
loop?