I don’t really understand what you’re trying to do in your PHP.
Simplest way (and, in my opinion, the more elegant solution, as this is a presentational issue) would be to do this in CSS. By default, WordPress puts plenty of helpful CSS in your <body>
tag, e.g.:
<body class="single single-post postid-53 single-format-standard logged-in admin-bar">
parent-pageid-{ID}
is a useful one if you have multiple child pages in a section.
So in your theme’s CSS file you can control the the colour of your navigation bar according to what post, page or category is visible – e.g.
.page-id-1 .top-navigation {
background-color: blue;
}
.page-id-2 .top-navigation {
background-color: green;
}
Related Posts:
- Customizing Only a Specific Menu using the “wp_nav_menu_items” Hook?
- Using a menu walker add a custom item at the end of the menu’s items
- Add settings to menu items in the Customizer
- Generate a Menu that Displays Child Pages using wp_list_pages() with the New Menu Functionality in WordPress 3.0?
- How Does The Walker Class Work?
- WordPress Shortcode in Menu Item Title
- Calling the Menu Title within wp_nav_menu array function
- How to hide an item from a menu to logged out users (without a plugin)
- Menu items (all menus) being deleted randomly on their own [duplicate]
- Large WP 3.0 menu times out and won’t save
- How to apply a class to the second word in menu list item?
- Put a wp_nav_menu inside another one
- How to switch between the Primary Menus programmatically?
- Add class to active top level (grandparent) menu item
- WordPress wp_nav_menu within iFrame
- Generate a Menu that displays all child pages of top level parent
- Terms with more than one occurrence have current-menu-item class in menu
- Check if wp_nav_menu items have submenus
- start_lvl on Walker is not working
- How to get Menu Navigation Labels to output HTML
- custom menu not found
- wp_nav_menu doesn’t generate parent/ancestor classes
- WP_nav_menu fallback not working
- How to conditionally register nav menus with advanced custom fields?
- Pending Custom Link in Menu
- Benefits of using Nav Walker?
- Use walker for specific menu, or avoid use of walker
- Change where navigation bar links?
- Social Links Menu displays icon instead of text (twentyfifteen) [closed]
- has_children in custom nav_walker
- Does wp_nav_menu use the gettext filter?
- How to add #navbar to all page links?
- How to add wechat (weixin) link to navigation menu?
- Create search form in menu as in Twenty Fourteen theme
- Link that refers to a menu item with dynamically generated URL
- Change an li class name in a wordpress custom menu walker
- Custom walker nav – Add sequential number as class
- WordPress Custom Menus Issue
- Side Menu Icon Expandable
- wordpress responsive menu not dropping down
- delete all post with certain meta_keys
- Make nav-menus.php available for editor
- how to add custom wp_nav_menu class css selector to ul and a tags?
- Need Random Category Nav Menu
- Split into two seperate unordered lists with custom walker
- edit user menu item
- How to remove UL class in wp_nav_menu?
- Excluding a media query from specific pages
- Use dashicons in menu items?
- Menu Arrow for Child Element
- Custom menu with different behavior for every item
- Responsive dropdown menu with smaller screen sizes
- Adding an external link to wordpress menu
- Menu separator appearing on next line
- Add a counter beside menu item label
- Why is my array of nav menus returning empty?
- Problem in applying active class to wordpress menu
- Modifying the walker to insert parent’s description above sub menus
- How to create a complete mulilanguage site using xili language plugin?
- Add pages content to startpage through custom menu
- Can you exclude child pages of a specific parent within a navigation?
- WordPress Menu Problem
- My menu doesn’t fit in the default screen in the mobile version
- How can I remove the navigation menu from a Page that is not on the menu?
- get_nva_menu_locations returns only filled ones
- Add attributes to wrapper html generated by wp_nav_menu
- Get Each Menu Nav Label of Menus in HTML tag
- How can I click the parent menu then redirect to a special URL?
- Stuck while adding bootstrap navwalker class
- use value returned from shortcode as menu item
- How do I display menu in header without loosing styles?
- Hiding specific navigation menus from specific roles in admin panel
- Create a menu item with a blank title
- Navigation bar is on live site but nothing under menu so can’t add in a page
- WordPress menu issue after update
- Displaying a part of a menu containing the page the user is currently viewing and only it’s immediate children
- WordPress adding Question Mark to URL when using Hashtags in Nav
- Menu’s built in the edit theme view do not have active states set when on child pages
- Specific Page that convert Hyperlink to anchor in nav menu
- Removing menu item doesn’t get removed on edit post
- change link of menu particular tab
- Next page link via the custom menu
- How can I make this custom design menu work in wordpress?
- Active menu item / category
- Add different span class to wp nav menu
- Top menu link anchor href value change after online server?
- Menu not always using correct link
- Custom Menus and taxonomies
- Help with changing style of the extended menu drop down [closed]
- How we show pages in dropdown
- wp_nav_menu doesn’t show on index page
- How to add posts with thumbnails to my dropdown menu?
- Automatically add new posts to a specific menu name
- can anyone help me converting my static menu to wp_nav_menu I’am trying to add static menu item to WordPress Menu
- Elegant way of adding an CTA to a menu
- Two Homes In Navigation Menu
- Theme supports only one menu. I need to add more – but how? [closed]
- How to set menu Display location
- How to add custom menu to block theme?
- get menu name for the current displayed page