WPGet.blogspot.com

Show Subcategories on Class Pages in WordPress

Show Subcategories on Class Pages in WordPress

When utilizing WordPress as a CMS, and even as a weblog you may wish to show subcategories on class pages. There may be actually no simple method to do that and a few methods which might be on the market on the net has varied bugs. On this article we'll present you how one can show subcategories on class pages utilizing this hack.

Now the hack shared on Yoast’s blog is outdated and has bugs. The bug is that regardless that it reveals the subcategories on the class web page, however while you go contained in the subcategory, it doesn't present any class. With the hack we're sharing it'll present subcategories on the mother or father class pages, and while you click on on the subcategories, you'll nonetheless see different sibling classes within the mother or father class.

Okay if the above terminology didn't make sense to you allow us to clarify in one other method. Let's imagine you have got a high stage (mother or father) class known as Sports activities. You will have three subcategories below sports activities known as NFL, NBA, and MLB. Now when somebody arrive to your homepage, you'll solely see Sports activities class and different high stage mother or father classes. When somebody clicks on Sports activities, the classes will now present solely the subcategories below Sports activities, so it will likely be NFL, NBA, and MLB. Now with Yoast’s hack should you click on on NFL, every thing can be gone. With our hack while you click on on NFL, it'll nonetheless present NFL, NBA, and MLB.

So principally this hack is permitting you to Listing subcategories if viewing a Class, and brothers / sibling classes if in subcategory.

    <?php
    if (is_category()) 
    ?>
    <?php
    if($this_category->category_parent)
    $this_category = wp_list_categories('orderby=id&show_count=zero
    &title_li=&use_desc_for_title=1&child_of='.$this_category->category_parent.
    "&echo=zero"); else
    $this_category = wp_list_categories('orderby=id&depth=1&show_count=zero
    &title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID.
    "&echo=zero");
    if ($this_category)  ?>

Paste the above code wherever you just like the classes to show and modify it nevertheless you please. The code listed above, while you go to the subcategory from the principle class, hides the principle class and solely reveals the subcategories.

If you wish to present the principle class additionally on subcategory pages, simply take away the parameter depth=”1″ from the code above.

It is a very helpful and helpful class hack in relation to designing templates for WordPress specifically when utilizing WordPress as a CMS.

For additional particulars on parameters seek advice from WordPress Codex.

Tutorials