You could try this, loop through all the wp_nav_menu
items, get the post using the Object ID then test to see IF the content is empty. IF that content is empty then unset it from the items array:
function remove_empty_items( $items ) {
$blog_page = get_option( 'page_for_posts' );
$home_page = get_option( 'page_on_front' );
$postArr = get_posts ( 'post_type=post' );
foreach( $items as $key => $item ) {
$tmpPost = get_post( $item->object_id );
if( $item->object_id == $blog_page && empty( $postArr ) ) {
unset( $items[$key] );
} else if( $item->object_id !== $home_page && empty( $tmpPost->post_content ) ) {
unset( $items[$key] );
}
}
return $items;
}
add_filter( 'wp_get_nav_menu_items', 'remove_empty_items' );
Related Posts:
- Removing container from wp_nav_menu not working
- Using a menu walker add a custom item at the end of the menu’s items
- Check if page is in a certain menu
- Show current navigation path from menu
- Is it possible to add classes to list items in wp_nav_menu()?
- How do I fix this error: Warning: invalid argument supplied for foreach()?
- Exclude one item from wp_list_pages( $args );
- different Menu position showing same menu
- How to add bubble count in WordPress wp_nav_menu menu?
- Add class to active top level (grandparent) menu item
- Page not appearing in navigation bar
- Extend WordPress REST API to show slugs for menu items
- WordPress Custom Menu Widget Style
- Output menu navigation in php file
- Custom Post type menu highlighted in the archive/post
- Unclickable Menu Item Label in Custom Menu with Clickable Children
- Use walker for specific menu, or avoid use of walker
- Sort wp_nav_menu() by menu order
- How to have custom menu Item CSS Classes for wp_page_menu() or wp_list_pages()
- How to give custom classes to the WordPress Menu widget
- No “current-menu-ancestor” class added when browsing an archive page
- Does wp_nav_menu use the gettext filter?
- Problems Displaying Multiple WP Menus
- View h2 content tags of a page in sub menu
- How to add custom classes to more than one separate menu’s a link in wordpress?
- Change an li class name in a wordpress custom menu walker
- Separator for custom nav menu
- add_submenu_page function to show videos table
- Menu item with no page, but with dropdown
- How to test if blog page is active
- WordPress Custom Menus Issue
- wordpress responsive menu not dropping down
- Create a MegaMenu using a walker and the start_lvl function
- Nav and logo loading each time causing menu to move JointsWP – Foundation 6
- WordPress Plugin menu position overwrite other item
- Two different “Home” items in custom menus dashboard section
- How to use a start_el variable in start_lvl
- Need Random Category Nav Menu
- How to re-arrange the MENU of “Allure Real Estate Theme for Placester” wordpress theme?
- Using _s theme, menu changes do not affect header menu
- Change class-name on “current_ancestor” in wp_nav_menu
- Hide a menu on posts
- How to add a page to header that is outside of WordPress?
- Get current menu item label for specific parent menu [closed]
- Can’t create a SubMenu
- Add (grand)child pages automatically to nav menu
- Having trouble adding CSS class through menu to link
- Mobile screen does not display Primary navbar…just a three line icon [closed]
- How do I enable “metaboxes” by default in menu editor?
- The cart, checkout, and shop links not getting removed
- How to pass menu slug as a variable to wp_nav_menu?
- New Menu Item Type Meta Box
- Using Different wp_nav_menu theme_location based on page id (or a parent id)
- Display Menu Navigation Label
- Menu is not displaying on the site navigation [closed]
- Dynamically get the meta key value of a menu item based on it’s post ID
- Does this function wp_get_nav_menu_object check if a nav menu exists or is supported?
- Search form as list item in menu?
- Most allowed Taxonomies in Appearance->Menus
- Updated WordPress, now Navigation is Messed Up
- wp_nav_menu removing containing UL with ‘items_wrap’ => ‘%3$s’ not working
- How to show only current item children using wp_nav_menu?
- Option to create menu is missing
- New menu is not showing in api response
- Newly created header is not visible on website
- Copied site has wrong menu URL behaviour
- Removing wp admin menu item on top bar of admin backend
- Nav_walker add id to dropdown ul
- Navbar is hidden behind carousel when using wp-boostrap-navwalker
- WordPress nav-menus not finding page
- Sub Menus will not work
- Primary menu items are dissapearing
- Active Current Menu – Adding class to child theme
- WPML CMS Nav and Custom Nav_Walker Undefined Property Notices [closed]
- Add data-track to links in menu
- Is it possible to seperate wordpress menu by different div?
- How to add odd and even classes to all nav links through walker?
- Output sort number in wp menu list item?
- Check in walker if current page is descendant
- Add a non-dynamic phrase/character after a page title?
- Primery Menu Configuration problem
- how to display wordpress navbar with top menu outside wordpress system
- Active page link not getting class assigned
- Removing menu item doesn’t get removed on edit post
- Menus not showing in the admin menu, after save
- Customize Walker_nav_menu to show posts if item is category
- Top menu link anchor href value change after online server?
- Add Protocol to Custom Menus
- How to handle change the appearance of ‘categories’ box on dashboard?
- menu doesn’t show in different languages
- How can I stop wp_nav_menu from returning extraneous root links?
- How to add posts with thumbnails to my dropdown menu?
- How to make an anchored page in a navigation menu tab
- Change menu item order
- Menu System: Custom Links
- Resolve the debugs
- Formatting WordPress Menus (without bullets, inline with text)
- Change Menu Order
- Two Homes In Navigation Menu
- How to change mobile menu (toggle) icon in WordPress – Full Site Editing?