Support » Plugins and Hacks » wp_list_categories: highlight current category when viewing single post

wp_list_categories: highlight current category when viewing single post

  • Hi all,

    since I need to use wp_list_categories in a site’s navigation, I have realized what I consider a bug in this function: it does not allow for highlighting the current category when I’m looking at a post.

    I’m not fond of fiddling with core files, so is there any way I can write a plugin or a functions.php function to add this “current-cat” style to the category list?

Viewing 15 replies - 1 through 15 (of 26 total)
  • warmbeer



    looks like “high noon” for dissolving the problems one can see over meanwhile 2 years or so while reading the “decoupling” and “consult hierarchy” comments in the php core.

    the whole mess with the category-template is fermenting since the devolopment focuses on tags and widgets.

    i have to find a solution for this within the next few days and will post this here. but smells like it will be a greasy hack. stay tuned!



    Perhaps the Codex does not display the parameter that the function wp_list_categories currently can handle.

    I just stumbled about the parameter current_category.
    You just have to set the current Category-Id for this.

    Try this in your Category-Template in your Templatefolder:

    $catsy = get_the_category();
    $myCat = $catsy[0]->cat_ID;

    Hope this helps. For my purposes it is working, though there could be more Functionality with collapsing Flaps. May be in the future…

    AWESOME !!
    That’s exactly what I needed, and it works like a charm. It’s actually in my header.php.

    Now, since I use a “home” before and a “wp_list_pages” after the wp_list_categories tag, I got weird results when I was on a page or on home.
    Here’s my addition to your code:

    if  (!is_page() && !is_home()){
    	$catsy = get_the_category();
    	$myCat = $catsy[0]->cat_ID;
    	$currentcategory = '&current_category='.$myCat;

    It checks if the page we’re on is NOT a Page and is also NOT home. In this case, it adds the current_category bit to the list_categories tag.

    Thank you so much warmbeer!


    for now 😉 let’s see what will happen with WP-Version 2.7.
    Glad to help You!

    Royal Bavarian Regards!

    This is exactly what I was looking for!
    I have my categories appearing in the footer like this:

    <?php if (is_home() || is_category() || is_single()) { ?>
     <ul class="categories">
       <?php wp_list_categories('title_li=&depth=1'); ?>
    <?php } ?>

    so my question is: where exactly do I put the code you have provided?

    never mind, got it

    2.7 is a great update, but it doesn’t seem to give more love to categories.

    how would I have to alter warmbeer’s code to allow for a .current-cat-parent class?

    warmbeer you’re awesome, cheers


    I’d like to know this too. Had any luck?

    your solution doesn’t work properly on deeper levels ;/ (I tried when [0] from array is on 3rd level deep). any ideas?

    I didn’t get this the first time round.

    Warmbeer… thank you so much!

    how do you fix the issue with tag cloud? When click to a tag cloud, both home item and others items are highlight.

    thank you so much!


    Looks like current_category is now listed as a parameter of wp_list_categories()

    Btw thanks, Warmbeer, this is the solution I was looking for with a similar problem.

    The problem I’m working out is that:

    $catsy = get_the_category();
    $myCat = $catsy[0]->cat_ID

    $catsy[0] is not always what you’re looking for. For example sometimes is a subcategory when I’m browsing a parent, and sometimes it doesn’t really show up.

    A partial solution is to replace $catsy[0] with $catsy

    $catsy = get_the_category();
    $myCat = $catsy->cat_ID

    This works properly with child categories and if a parent category is clicked then the parent and all the children are highlighted (that is show class=”current-cat”.

    In the end I may want the highlighting to only be on precisely what single_cat_title(] would display…. however having the parent and all the children highlighted when browsing a parent could actually be a feature……

    (I do change the order of the categories and subcategories from the defualt, so maybe that’s why $catsy[0] behaves badly for me.)

    I stumbled upon this and I just want to say thank you! What warmbeer posted worked perfectly!

Viewing 15 replies - 1 through 15 (of 26 total)
  • The topic ‘wp_list_categories: highlight current category when viewing single post’ is closed to new replies.