As seasoned WordPress developers with years of experience building CMS sites and custom themes, we've often needed to enhance category navigation. Displaying subcategories on category archive pages improves user experience, but many online solutions fall short—especially the outdated Yoast method, which breaks sibling category visibility on subcategory pages.
Our proven technique fixes this: it shows subcategories on parent category pages and sibling categories (brothers/sisters) when viewing a subcategory. For example, imagine a parent category "Sports" with subcategories NFL, NBA, and MLB. On the Sports page, users see NFL, NBA, and MLB. Click NFL, and it still displays NFL, NBA, and MLB—no vanishing acts.
This code dynamically lists child categories for parents or siblings for children. Add it to your theme's category.php template (or via a child theme/plugin for safety):
<?php
global $cat;
$this_category = get_category( $cat );
if ( $this_category->category_parent ) {
$this_category = wp_list_categories( 'orderby=id&show_count=0&title_li=&use_desc_for_title=1&child_of=' . $this_category->category_parent . '&echo=0' );
} else {
$this_category = wp_list_categories( 'orderby=id&depth=1&show_count=0&title_li=&use_desc_for_title=1&child_of=' . $this_category->cat_ID . '&echo=0' );
}
if ( $this_category ) { ?>
<ul>
<?php echo $this_category; ?>
</ul>
<?php } ?>Paste this where you want the list to appear. Currently, subcategory pages hide the parent and show only siblings. To include the parent category too, remove &depth=1 from the second wp_list_categories() call.
This robust solution has powered navigation on numerous production WordPress CMS sites. For full parameter details, consult the WordPress Codex.