WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Make wp_nav_menu output "current_page_item" in navbar when viewing a single page (5 posts)

  1. Psylicium
    Member
    Posted 3 years ago #

    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 :)

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

  3. Psylicium
    Member
    Posted 3 years ago #

    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`?

  4. 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?

  5. Psylicium
    Member
    Posted 3 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic