WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] wp_nav_menu exclude post or page by ID (5 posts)

  1. lonchbox
    Member
    Posted 1 year ago #

    Hi,

    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.

    <?php
    			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
    				) ); ?>
    			</aside>
    		<!--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.

  2. bcworkz
    Member
    Posted 1 year ago #

    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().

  3. lonchbox
    Member
    Posted 1 year ago #

  4. bcworkz
    Member
    Posted 1 year ago #

    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 :)

  5. lonchbox
    Member
    Posted 1 year ago #

    Wise advice! and nice lesson :) thanx I will more info of wp_get_nav_menu_items :)

Topic Closed

This topic has been closed to new replies.

About this Topic