WordPress.org

Support

Support » Themes and Templates » [Resolved] Make wp_nav_menu output "current_page_item" in navbar when viewing a single page

[Resolved] Make wp_nav_menu output "current_page_item" in navbar when viewing a single page

  • Hi. I’m in the process of making a new theme, but I seem to have a slight issue with wp_list_pages() when viewing a single page (single.php).

    You see, I am using the current_page_item css selector to highlight the current page shown in the navigation bar. It works fine when I click my pages, ie. Webdesign, Blog and Profile: the current_page_item is added to the page that’s displayed.

    But if I for example want to view a blog post displayed by single.php, the current_page_item selector is not applied, and no menu items are highlighted. I would like to still have the current_page_item added to the “Blog” item in the navigation, so when I’m viewing anything that has to do with my blog, the item stays highlighted.

    Of course, I could manually create the navigation and define if and when current_page_item should be applied, like so:

    <li class="<?php if ( is_page('blog') || is_single() && in_category('blog')) { echo 'current_'; } ?>page_item"><a href="/blog/" title="Blog">My blog</a></li>

    – but I would rather use wp_nav_menu, and use only wp-admin if I edit pages, instead of having to edit the template code. Is there any way – either in wp_nav_menu() itself or in functions.php – I can define conditional statements or similar like the above to control current_page_item?

    English isn’t my native language, so I hope you understand what it is I’m trying to achieve 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • Christine Rondeau

    @crondeau

    Volunteer Forum Moderator

    When the single post is selected, you should be able to use current_page_parent and style it similarly to your current_page_item.

    I see no current_page_parent in the source. Is a snippet of code required anywhere to make WP output this selector? Of do I have to enable that new and fancy admin menu-thingy in functions.php`?

    Christine Rondeau

    @crondeau

    Volunteer Forum Moderator

    I didn’t think so, I just used wp_page_list() and it was generated automatically.

    I did set a page called blog and then under reading choose a static home page and then chose the blog page to display posts.
    Maybe this is what’s needed?

    Perhaps also the theme you are using is overwriting it.

    Can you provide a link to your site?

    I believe that did the trick. I now see the current_page_parent. On to styling. Thanks, Christine 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] Make wp_nav_menu output "current_page_item" in navbar when viewing a single page’ is closed to new replies.