3

I'm using the built in menu manager to create my menues and I disaply them by using wp_nav_menu().

I'm wondering, is it possible to remove certain menu items from being displayed based on some specific condition?

1

3 Answers 3

4

Use the wp_nav_menu_objects filter to adjust the menu items using whatever conditions you like. The filter will receive and return an array of the menu items.

2
  • I tested that earlier. But I was not able to remove the object from the array. I tested using unset($some_name_obj).
    – Steven
    Aug 8, 2011 at 15:04
  • You were probably unsetting it wrong. You have to unset it in the array itself to remove it from the array. Like unset($whatever['item']) then return $whatever.
    – Otto
    Aug 8, 2011 at 19:54
1

Instead of using wp_nav_menu(), you can try using wp_get_nav_menu_items which will return a list of menu items, then you can have conditional statements to pick an choose which to display.

http://codex.wordpress.org/Function_Reference/wp_get_nav_menu_items

1
0

if our menu is pages you can use Exclude Pages from List

Just use the id of pages sepearated b comma

<ul>
  <?php wp_list_pages('exclude=17,38' ); ?>
</ul>

UPDATE

Try this one

Go to Appearance -> Menu and then from there click "Create Menu" then add menus that you want to include and you can set a Location for menus with this code below try to explore

http://codex.wordpress.org/Function_Reference/wp_nav_menu
3
  • No. I'm managing my menu items from wp-admin and must use wp_nav_menu(). I can't hardcode what pages are to be exluded in the menu, because it depends on some certain conditions.
    – Steven
    Aug 8, 2011 at 9:18
  • try to my answer update I hope it helps Aug 8, 2011 at 9:32
  • Using wp_list_pages without the include parameter, will list all pages I have. That kind of defeats the purpose of having a custom menu. The whole point is selecting specific pages to be in the menu, by using the menu admin in WP. In order to list pages added through the menu admin, one must use wp_nav_menu or wp_get_nav_menu_items.
    – Steven
    Aug 8, 2011 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.