Showing subcategories “dynamically”

Published: On Thursday, June 7, 2007

This piece of code is actually a response on a query on the Czech WordPress support forum, where a guy asked how to display a list of categories that are children of the just active (browsed) one. I didn't have a clue, although I thought this could be a very common request: on many pages you can see the category navigation that after the activation of one particular item gets more concrete, to put it differently, the subcategories are shown as an offer while browsing a category.
<?php

// outputs usual category-list

wp_list_categories('title_li=<h2>Categories</h2>');

// outputs subcategories in same way, if there are any (if you're browsing a parental category)

$has_children = (bool)(get_category_children(intval(get_query_var('cat'))));

if (is_category() AND $has_children) {
        wp_list_categories('title_li=<h2>Subcategories</h2>&child_of=' . intval(get_query_var('cat')));
}

?>

For more comfortable testing I recommend to add hide_empty=0 paramater and perhaps $has_children variable needs an explaining note: It's here because a try to output the list of subcategories can end with an visible error, if there are found no subcategories (= if you would try to show subcategories of a category having not a one children :-)).


Comments

  1. 1.  Toxic wrote: On August 23., 2007 comment number 1

    What would be good would be as above, only children of active cat shown along with the active category somehow being automatically placed at the top of the category list.

  2. 2.  David wrote: On February 26., 2009 comment number 2

    It is a very old post you have here, but you know still there are many people who are looking for such helping codes in net.

    I am working on a site in moment and was fighting with the display style code of categories and subcategories.
    Actually I am really new to all this coding stuff… , I wanted to display the categories and subcategories in the left sidebar.

    The problem occurs in displaying as I want to display the Current_Category ( With subcategories ).
    I have also PAGES in the sidebar which are displayed fine if I am in the CURRENT PAGE. It is highlighted properly through css if I use
    .left ul li.current_pa­ge_item a
    this code.

    BUT this don't works with CATEGORIES AND SUBCATEGORIES properly.

    Because if I am on current category it highlights the subcategories as well with the same code. And I am unfortunately not finding any solution for that ( searching for this A FEW DAYS now).

    Any help will be appriciated from my side, if someone can help. Thanks in advance.

    David.

  3. 3.  Kahi [author] wrote: On February 26., 2009 comment number 3

    ↪ David maybe you search for my another article – Highlight active categories (while browsing a post) href=„http://­kahi.cz/wordpres­s/make-active-categories-different-while-browsing-a-post/“ rel=„nofollow“>http:/­/kahi.cz/wordpres­s/make-active-categories-different-while-browsing-a-post/

  4. 4.  marie antoine wrote: On March 4., 2009 comment number 4

    Hi,
    I saw your post and I would like to ask you a hand.
    I am looking for displaying the subcatergories under my categories and I have no clue.
    I wonder if you could help me?

    Thanks a lot

  5. 5.  Kahi [author] wrote: On March 4., 2009 comment number 5

    ↪ marie antoine Question is, what you'd like it to look like. I don't get it from your description.

  6. 6.  Eastwood wrote: On April 11., 2009 comment number 6

    That was very helpful – even more so than the WordPress Codex. Thanks!

  7. 7.  Shane wrote: On May 2., 2009 comment number 7

    If I have categories going 3 deep, for example:

    A > A1 > A2

    And I click on the category A, and use your code to show the subcategories, how would I change the code to only show the A1 set and not show the A2 until I browse to A1?

    Hope that's not too confusing. Thanks!

  8. 8.  Kahi [author] wrote: On May 2., 2009 comment number 8

    ↪ Shane I'd add &depth=1 parameter into wp_list_categories(...). See function description: http://codex.wor­dpress.org/Tem­plate_Tags/wp_lis­t_categories

  9. 9.  Abu Farhan wrote: On July 15., 2009 comment number 9

    How can make category and Subcategory Navigation ?. In my problem I only Have parent and one child category. I want always show parent category and then if I click parent category will show child category, when I click Child Category the navigation still same.

  10. 10.  Kahi [author] wrote: On July 15., 2009 comment number 10

    ↪ Abu Farhan You may get inspired by the example above but in the first wp_list_categories add parameter &depth=1

I quit working with WordPress, comments are closed. My plugins will not be updated any more – at least not by me. Feel free to modify my source codes though… Also I am not able to provide support, sorry. –Kahi