Assuming you are loading a single piece of content like one Post (unlike the homepage where you’re combining multiple Pages with JS), something like this should do it:
<script type="text/javascript">
$(document).ready(function(){
// get each menu item
var hasHighlight = 0;
// loop through to see if any have "current_page_item" class
$("#nav ul li").each(function(obj) {
if($(this).hasClass("current_page_item")) { hasHighlight++; }
});
// if none of the items are highlighted using "current_page_item" class
if(hasHighlight == 0) {
$("#nav ul li:first-child").addClass("current_page_item");
}
})(jQuery);
</script>
Enqueue this sitewide so that anytime there is a menu with id “nav” it will check and add a highlight to “Home” if nothing is highlighted.
Related Posts:
- How to load scripts/styles specific for a page
- Remove Container Element From wp_nav_menu() Markup
- Breadcrumbs showing Parent and Child Pages
- Return only top-level navigation items from a menu using wp_get_nav_menu_items
- Child Theme Not Overriding Parent Theme
- Menu fallback “menu_class” rendering a “div” instead of a “ul”
- How do I create page navigation linking to each H2 within the page? [closed]
- Why does the ‘wp_nav_menu’ function work only until a menu is created?
- Hide Hamburger Menu On Specific Page (Front Page/Home Page)
- Custom Static Links For Specific Menu Right Before/Next The wp_nav_menu Function now working
- Child Theme files – what is needed?
- Categories as main menu items and subcategories as nested lists
- Not all CSS elements transferring via parent to child?
- Some questions about how proper add 2 CSS file in a WordPress theme?
- Filter nav menu items HTML tags and wrap inner text with span
- retrieves menu and converts it to a readable array
- Secondary Menu and Logged In Users
- Multiple navigation menu in same location
- current_page_item is missing inside wp_nav_menu
- define css class in functions.php
- How would I make a function to dynamically determine if user is logged to change navigation menu text?
- Bizarre Child Menu Issue on WP Site
- Header menu aligned right on all pages except for single-post page [closed]
- CSS style and app.js not loading
- How to automatically add counted number to navigation menu items?
- Setting Up Child Theme To Take Priority
- How to add widget in main menu
- Random text changing to weird icons in both admin & front end
- How to add or remove borders in navigation menu?
- Why don’t ‘wp_nav_menu’ CSS classes work until a menu is created?
- Child theme – Overriding ‘require_once’ in functions.php
- Add custom classes to anchor in wp_nav_menu
- Issue with get_theme_mod returning a blank value instead of the saved value
- Using wp_get_nav_menu_items() to list children of children pages
- How to move parent li to end of child ul
- Hourly Routine Not Firing ( wp_schedule_event() )
- Custom menus displaying all pages instead of set pages
- Theme Demo in live Site
- Two different menus for two different locations?
- Customising widget titles/headings in WP 5.8
- How to add CSS style sheet dynamically in wordpress
- Displaying wp menus by name without using theme locations
- CSS stylesheet not loading
- Editing or filtering the output of the Genesis navigation
- Multiple CSS body classes to specific page I.D.’s – is this possible?
- CSS for menu item added via functions.php
- Replace menu links with # and add name to its li
- Function issue with Walker_Nav_Menu [duplicate]
- How can I change the do_action function in footer (copyright area)
- changing behaviour of get_search_form
- Make parent page only link to first subpage
- Grey out nav buttons unless is_user_logged_in()
- wp_nav_menu, walker class, categories as classes of li
- WordPress website Rs changed to currency symbol
- Functions in custom theme
- Please Explain the Importing Process of Multiple Stylesheets for Child Themes
- How to get correct URLs in network wide menu (Multisite)?
- append to existing parent theme function
- different style sheet for just one page template
- How Do I Add Images To Main Navigation Menu
- Add login hyperlink to secondary navigation menu
- Dropline menus — seperators between children only?
- Functions php shortcode for displaying main menu with no child items
- How to make a dynamic css class whose name changes every visit to confuse scraper
- I can’t add CSS with functions.php
- why is this function firing on all child menu items as well?
- Custom styles in Tiny MCE with an external CSS file
- why can’t i add front.css to my frontpage.php
- Different methods of adding menu support to custom theme
- Getting out side of wp root folder from function.php using absolute path
- How to hightlight all ancestor menu items of a child page NOT displayed in menu
- Page Template Won’t Load Correct CSS File
- Why a SlideShow (made using JQuery FlexSlider) can’t work if I load it form functions.php but work if I load it from my footer.php file? [closed]
- How Can I add a menu to the theme from withen the function.php file
- Astra theme – cannot set short product description under the product title (product name)
- Completely Disable WordPress RSS Feeds
- Combine page types and Custom Taxonomy in a functions.php command
- Class parameter in get_avatar args doesn’t get added to output
- Stylesheet does not load despite functions.php
- Remove clickable Link of WordPress Site Logo from Woocommerce Single Product page
- Help using ShortCodes to style whole chunks of the post
- Combine multiple menus using the filter wp_nav_menu_items
- Disable Top Nav Bar on Mobile
- Making my main menu a hamburger menu on all platforms
- Getting back a blank WordPress site following functions code edit
- Insert menu into theme location depending on user logged in/out status
- How to pass a variable from a template page to the child theme functions.php
- Calling the first & last post by category in custom post type
- Changing the color of post title [closed]
- Register a widget area when a theme option has been saved?
- Additional Sidebar
- individual images as navigation design TwentyTen
- How do I get my nav menu to show sub pages?
- Notice: Use of undefined constant REQUEST_URI – assumed ‘REQUEST_URI’ in ….functions.php on line 73
- How can I append and prepend something to all post hyperlinks without using ::before or ::after? PHP hook solution?
- Image width issue in IE [closed]
- How do I make my child theme’s CSS update when I save it?
- Site navigation sits unstyled for a second on page reload [closed]
- Adding function to function.php error 505 [closed]
- Prevent primary navigational menu from expanding during page load (WordPress / Astra Theme)