How To Display Subcategories on Category Pages in WordPress

[easy-social-share counters=0 style="button"]

When you use WordPress just like a CMS, and even as a blog you may need to show subcategories on category page. There may be actually not any simple method to try thisbut some techniques that are you can get on the internet is a number of problems. In this post we can easily explain the best way to show subcategories on category page by using this hack.

Right now the hack shared on Yoast’s blog is old used or is problems. The bug is the even when it displays the subcategories on the category page, however when you are going inside the subcategory, it will probably not display any kind of category. With the hack we are now sharing this will display subcategories on the parent category page, so when you click the subcategories, you can also find out different sibling categories in the parent category.

Okay in case the above conditions do not important to you allow us to clarify in the other way. Lets speak you can find a great stage ( parent ) category known as Sports activities. There are 3 subcategories under sports known as NFL, NBA, or MLB. Right now whenever someone show to your home-page, you can only view Sports category or other first stage parent categories.

 If someone clicks on on Sports, the categories will now display only the subcategories under Sports, so that it can be NFL, NBA, and MLB. Complete with Yoast’s hack in case you click NFL, every thing is going to be finished. With our hack whenever you click NFL, it is going to still display NFL, NBA, or MLB.

In that case simply this hack is permitting you to List subcategories in case viewing a Category, or brothers / stable mate categories in case in subcategory.

<?php

    if (is_category()) {

    $this_category = get_category($cat);

    }

    ?>

    <?php

    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 the above code where ever you prefer the categories to show and then adjust it then again you you need to. The code posted above, whenever you take a look at the subcategory from the main category, hides the main category or only displays the subcategories.

When you want to display the main category and also on subcategory page, simply clear off the parameter depth=”1″ from the code above.
This can be a really helpful and also useful category hack when you are design and style web templates for WordPress in particular when you use WordPress like a CMS.

Submit a comment

Pin It on Pinterest