Support » Plugins » Hacks » wp_nav_menu exclude post or page by ID

  • Resolved Pancho Perez



    I know there is any mention to exclude parameter for wp_nav_menu in the codex, but also found some posts saying this is posible, I try many of it and donยดt work.

    In my case Iยดm creating a custom widget for my theme, and this widget shows custom menu, what I want is if is in a page which are listed in the menu the item of this page was remove form the menu, so first I get the ID and I use this variable in the exclude.

    			global $wp_query;
    			$page_id = $wp_query->post->ID;
    		<!--BEGIN #joinn-us-->
    			<aside id="related-content" class="widget panel">
    				<h4 class="widget-title"><?php _e('Related Content','udla'); ?></h4>
    				<?php wp_nav_menu( array(
    					'theme_location'=> 'related-menu',
    					'menu_class'	=> 'side-nav',
    					'container'		=> '',
    					'container_class'	=> '',
    					'exclude' => $page_id
    				) ); ?>
    		<!--END #join-us-->

    Should I use the fallback_cb parameter?
    Or do I need to set a function to use this parameter?

    Thanx for any help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz


    If you review the source code of wp_nav_menu() and all of the functions called from there, you will see there is no capability to handle an ‘exclude’ parameter. It may be possible to filter something so that it would work, but WP out of the box will not handle it.

    You should not need to use fallback_cb if the function is assured of finding nav menu items. It’s purpose AFAIK is to provide functionality for dynamic menus where there is a possibility no menu items would be defined.

    I have not tried this, but I think the best approach is to hook into the ‘wp_get_nav_menu_items’ filter. Your filter callback is passed an array of menu items from which the menu HTML will be generated. If you can find a way to match a menu item to the current page, you simply can unset that particular item to remove the menu pick from the resulting menu HTML. Return the menu item array less that one item and it all should work out when you call wp_nav_menu().

    Moderator bcworkz


    I don’t know what to tell you other than you can’t believe everything you read on the internet. Perhaps the author has a plugin or theme that filters the function that adds this functionality on his system, unknown to him. I can assure you the source code is not capable of interpreting ‘exclude’ arguments out of the box. My advice stands, the best approach is to filter ‘wp_get_nav_menu_items’.

    As this is the internet, if you choose to not believe me, I understand and will not be offended ๐Ÿ™‚

    Wise advice! and nice lesson ๐Ÿ™‚ thanx I will more info of wp_get_nav_menu_items ๐Ÿ™‚

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘wp_nav_menu exclude post or page by ID’ is closed to new replies.