WordPress.org

Forums

[resolved] current-cat in single post view with multiple categories (8 posts)

  1. Paul Wright
    Member
    Posted 5 years ago #

    Hello,

    I am using wp_list_categories to make my site navigation, both an upper bar and a lower bar with the children if they exist.

    I want to highlight the current category and at first I was using the built-in class that WordPress assigns, but then I wanted to get it to work even in single (post) view, so I used this:

    <?php
    	if  (!is_page() && !is_home() && !is_single()){
    	$catsy = get_the_category();
    	$myCat = $catsy->cat_ID;
    	$currentcategory = '&current_category='.$myCat;
    }
    
    	elseif (is_single()){
    	$catsy = get_the_category();
    	$myCat = $catsy[0]->cat_ID;
    	$currentcategory = '&current_category='.$myCat;
    }
    
    	wp_list_categories('depth=1&title_li=&orderby=id&exclude=1,5,6,19,20,21,22&hide_empty=0'.$currentcategory);
    	?>

    Which works great if the post is only categorized in one category...if you have it in several, it seems to highlight only the first alphabetical category.

    How can I make this more robust?

    To be more specific for my case: I am excluding certain categories (see above code) and if one of those comes before a category I am using, nothing gets the active class (because it only gives it to the first one, alphabetically).

    I would be interested in highlighting all the categories the post falls into, or even just having it skip the categories I am excluding from the navigation...then at least one navbar item would get the active class for each post (for visual consistency).

    Thanks!

  2. Paul Wright
    Member
    Posted 5 years ago #

    Any help on this would be greatly appreciated.

  3. zeemiDesign
    Member
    Posted 5 years ago #

    I'm working on a somewhat similar problem, but didn't come up with a solution yet.
    I thought about modifying the wordpress core-files and extending the native function that assigns the "current-cat"-class to the active category on archive pages as well to the post, but it obviously isn't within the category-template.php and I have no clue where else to look.

  4. Paul Wright
    Member
    Posted 5 years ago #

    Check out this similar thread I have going, it might be of some use:

    http://wordpress.org/support/topic/328775

  5. karl19
    Member
    Posted 5 years ago #

    @signyourbikeout,

    I was looking to fix this as well and came across this tutorial which then linked to this plugin.

    Instead of running the plugin (it's just one function), I added it to my functions.php and now I get a current class for a category even when on a single page. Not sure if it works when having more than one level of categories, but might be worth giving it a try.

  6. Paul Wright
    Member
    Posted 5 years ago #

    Hi karl19,

    Thanks, yeah I did actually end up coming across that post and using it on my site. It works for single.php, but does not handle multiple categories very well. I have a function in place now that seems to handle it 'most' of the time.

    Not 100% amazing, but doesn't cause any errors and half the time adds the active class to multiple parents plus one child

  7. pixeline
    Member
    Posted 5 years ago #

    I found this "Kahi’s Highlight Used Categories" plugin to be of great help. Handles multiple categories perfectly.

    Make sure you add a .used-cat class in your style.css to behave similarly as the .current-cat class provided by wordpress.

  8. Lucas Cepeda
    Member
    Posted 5 years ago #

    Thank you pixeline! Seems to work great :)

Topic Closed

This topic has been closed to new replies.

About this Topic