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?
- WordPress website Rs changed to currency symbol
- change the url paths in the stylesheet dynamically
- Custom Nav Walker sub-menu
- Is there an alternative to get_template_directory_uri()?
- How to fetch the name of the active menu?
- create filter in functions.php
- Add Custom Background Properties to Customizer
- Static page to one page- menu inactive items highlighted
- Functions in custom theme
- Please Explain the Importing Process of Multiple Stylesheets for Child Themes
- Replace a menu with widget or a custom template file programmatically
- Parse error after modifying the template menu [closed]
- WordPress PHP Conflicting User Sessions
- Detecting classes, adding widgets, and adding divs in with a Nav Walker
- How to get correct URLs in network wide menu (Multisite)?
- How to create different navigation for registered users in WooThemes Canvas?
- Change CSS for logged in users
- Removing specific menu items?
- Redirect to another page using contact form 7? [closed]
- append to existing parent theme function
- Function that outputs second theme url
- How to stop wordpress from injecting hard-coded style into image attachments with captions
- different style sheet for just one page template
- How Do I Add Images To Main Navigation Menu
- Custom styles.css sheet for visual editor
- Is my code correct to enqueue CSS on a specific page?
- header_image() with CSS
- display menu as table layout
- unnecessary request call for js and css after calling wp_dequeue_script
- How do I hook into the container of wp_nav_menu?
- Split Media Queries in different files!
- Add login hyperlink to secondary navigation menu
- Turn get_posts as string into an array for use in theme admin options
- Menu Arrow for Child Element
- New custom menu created. how to apply it?
- problem loading stylesheets to wp_head dynamically
- Dropline menus — seperators between children only?
- How to buid wordpress menu with auto width sub-menu items in dropdown menu without any items sharing one line [closed]
- Functions php shortcode for displaying main menu with no child items
- Nav menus all outputting the same links
- How to make a dynamic css class whose name changes every visit to confuse scraper
- Change menu based on page template via functions.php
- Can’t switch to a child theme using filters template, option_template and option_stylesheet
- Show some menu sub items as dropdown under a menu item
- How to add css to single navigation text? [closed]
- Redirect website domain (including all pages) to external URL after Popup message (few seconds)
- How to add custom li item to wordpress menu
- How to insert class in each list of categories?
- Add Login/Logout Menu Item to Primary Nav “My Account” Submenu [Woocommerce] [closed]
- Parent theme styles overriding child theme CSS [closed]
- I can’t add CSS with functions.php
- Menu Custom Data Attributes
- Theme JS is available but theme CSS isn’t
- Proper Way to Load stylesheet on Condition
- This code works, but the way I integrated it is breaking the media uploader. How can I integrate it properly?
- Full screen nav menu block with avada theme
- Getting the teaser text without overriding global variables
- 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
- How to modify mobile nav menu text in theme
- get_template_directory_uri() links to child theme not parent
- Different methods of adding menu support to custom theme
- “options.php” not found
- Adding a section in line at the end of the main navigation menu in php
- remove/hide wp-editor
- How to override this theme function in child theme
- Getting out side of wp root folder from function.php using absolute path
- wp_nav_menu work in functions.php but not in the theme
- How to register a menu based on a ACF condition