WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Highlight Current Category in Navbar on Single Page (18 posts)

  1. bmg1227
    Member
    Posted 6 years ago #

    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='); ?>

  2. bmg1227
    Member
    Posted 6 years ago #

    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"';}?>>

  3. moshu
    Member
    Posted 6 years ago #

    in_category?

  4. bmg1227
    Member
    Posted 6 years ago #

    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"';}?>>

  5. moshu
    Member
    Posted 6 years ago #

    in_category should work on single
    is_catgeory should work on cat archives

    EDIT. Seemingly, we posted in the same time...

  6. bmg1227
    Member
    Posted 6 years ago #

    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?

  7. Jeremy Clark
    Moderator
    Posted 6 years ago #

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

    This is untested.

  8. bmg1227
    Member
    Posted 6 years ago #

    Jeremy, thanks man, this worked.

  9. bongkph
    Member
    Posted 6 years ago #

    what if I fulled categories with the following code:

    <ul id="nav">
    <li><a href="http://www.site.com/category1">Category1</a></li>
    <li><a href="http://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

  10. incomehero
    Member
    Posted 6 years ago #

    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?

  11. alainS
    Member
    Posted 6 years ago #

    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?

  12. Anonymous
    Unregistered
    Posted 6 years ago #

    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 http://www.quoteside.com

    Regards,
    JOAN

  13. alainS
    Member
    Posted 5 years ago #

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

  14. Anonymous
    Unregistered
    Posted 5 years ago #

    hi joandiego,

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

  15. alainS
    Member
    Posted 5 years ago #

    Thanks!
    Works like a charm in 2.6 too.

  16. Anonymous
    Unregistered
    Posted 5 years ago #

    I'm glad it works on 2.6
    Happy coding.

  17. heriz
    Member
    Posted 5 years ago #

    joandiego - wonderful! Thanks so much for that.

    The only problem I have now is the fact that the page_parent of a category page is the default posts page.

    In my stylesheet I have:

    #nav ul li.current_page_item a, #nav ul li.current_page_parent a, #nav ul li.current-cat a {
    		color: #AA0000;
    	}

    The parent menu item of a child page is still highlighted when on that child page, and the menu item of a category is highlighted when on a category page.

    But because, when on a category page, the default posts page is marked with 'current_page_parent', it means the default posts page menu item is highlighted AS WELL AS the category, when on a category page.

    Is there any way round this? So that I can still highlight parent menu items to a depth of 2 on both static pages and category pages, without the posts page highlighting when on a category page?

  18. mikedev
    Member
    Posted 5 years ago #

    joandiego,

    Your code works great, but it seems to work only for one level of child nesting. Do you think you could get it to work for three or more, or an infinite amount of child nesting?

    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.