• Resolved bmg1227

    (@bmg1227)


    Ok, now onto the next trick. I want to highlight the current category link in the navbar when viewing a post within that category, not just when the category “archive” page is being viewed.

    Here’s the current code I am using to pull category links in the navbar:

    <?php wp_list_categories(‘sort_column=name&title_li=’); ?>

Viewing 15 replies - 1 through 15 (of 17 total)
  • Thread Starter bmg1227

    (@bmg1227)

    Here’s what I have and it works for all single pages, how do I specify if I want it active on category ID 2 only?

    <li <?php if(is_single()){echo ‘class=”current-cat”‘;}?>>

    in_category?

    Thread Starter bmg1227

    (@bmg1227)

    Got that to work, trying to make that work for in_category as well as is_category. Just need to rewrite this so it also has is_category(5)) but I don’t know how to do both. Sorry Moshu, I’m a lame-o this morning.

    <li <?php if(in_category(5)){echo ‘class=”current-cat”‘;}?>>

    in_category should work on single
    is_catgeory should work on cat archives

    EDIT. Seemingly, we posted in the same time…

    Thread Starter bmg1227

    (@bmg1227)

    Right, that I know – but I’m hoping to get the blog link active when it’s doing either or. On the Blog category/archive page or when it’s a single post within the blog category. Make sense?

    Use an or statement
    <li <?php if(in_category(5) || is_category(5)){echo 'class="current-cat"';}?>>

    This is untested.

    Thread Starter bmg1227

    (@bmg1227)

    Jeremy, thanks man, this worked.

    what if I fulled categories with the following code:

    <ul id="nav">
    <li><a href="https://www.site.com/category1">Category1</a></li>
    <li><a href="https://www.site.com/category2">Category2</a></li>
    </ul>

    I cannot pull all categories because there are too many. Is there a way to pull only main categories? Thanks

    Like I understand you have to manually enter:

    <li <?php if(in_category(5) || is_category(5)){echo 'class="current-cat"';}?>>

    for every category that you have, is that right?

    But is there a way to do this automatically?

    I mean, is there a way to edit ‘category-template.php’ file and do something with wp_list_categories function, that it would automatically generate list with the effect mentioned above?

    I have almost the same problem. When i am on categories current category is highlighted…when i go to single post the category is not “current-cat” anymore.

    Is there a workaround to use “wp_list_categories” on single post to highlight current category or at least parent category in which the post is?

    Hi everyone!
    I was looking for the very same thing, but finding nothing on the web I built a fix myself.

    So this is for you if:

    – you are using wp_list_categories
    – you want to add the current-cat attribute when reading a single post

    What to do:

    in wp-include/category-template.php

    after line 313

    add:

    if ( is_single() ) {
    
    		$blork = get_the_category();
    		$r['current_category'] = $blork[0]->cat_ID;
    		}

    Result can be seen on https://www.quoteside.com

    Regards,
    JOAN

    Does your solution brake something elsewhere on blog or is safe to use? Tnx

    hi joandiego,

    Thank you for sharing. It’s exactly what I’m looking for.

    Thanks!
    Works like a charm in 2.6 too.

    I’m glad it works on 2.6
    Happy coding.

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Highlight Current Category in Navbar on Single Page’ is closed to new replies.