wp_nav_menu: show menu only if one exists, otherwise show nothing

Use has_nav_menu(), and test for theme_location, rather than menu_id: <?php if ( has_nav_menu( $theme_location ) ) { // User has assigned menu to this location; // output it wp_nav_menu( array( ‘theme_location’ => $theme_location, ‘menu_class’ => ‘nav’, ‘container’ => ” ) ); } ?> You can output alternate content, by adding an else clause. EDIT You … Read more

Custom Nav walker display current menu item children, or siblings on no children

This is the walker I used to display only children of the current menu item. Or the menu items siblings if it doesn’t have any children of its own. There are comments throughout the class explaining each section <?php class SH_Child_Only_Walker extends Walker_Nav_Menu { private $ID; private $depth; private $classes = array(); private $child_count = … Read more

Removing container from wp_nav_menu not working

[SOLVED] IT DOES NOT WORK when you are referring to an inexisting location. e.g. when you copied the code from somewhere else, or you haven’t created your menu or location yet in the dasboard. e.g. remove “, ‘theme_location’ => ‘primary’” from the following code: wp_nav_menu( array( ‘container’=> false, ‘menu_class’=> false, ‘menu_id’=> ‘ia_toplevel’, ‘theme_location’ => ‘primary’ … Read more

Error: Declaration of MyClass::start_lvl() should be compatible with that of Walker_Nav_Menu::start_lvl()

From class Walker_Nav_Menu: function start_lvl( &$output, $depth = 0, $args = array() ) Your child class must use the same signature: three arguments, the first one passed by reference. Every difference will raise the error you got. Note that $args defaults to an empty array, but you get an instance of stdClass, not an array. … Read more

Dynamically exclude menu items from wp_nav_menu

Method 1 You can add a constructor to your custom Walker to store some additional exclusion arguments, like: class custom_nav_walker extends Walker_Nav_Menu { function __construct( $exclude = null ) { $this->exclude = $exclude; } function skip( $item ) { return in_array($item->ID, (array)$this->exclude); // or return in_array($item->title, (array)$this->exclude); // etc. } // …inside start_el, end_el if … Read more

WordPress default menu in database

Menu by itself is a taxonomy in WP. It means that you can find all menus in wp_terms table, by running following query: SELECT * FROM wp_terms AS t LEFT JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id WHERE tt.taxonomy = ‘nav_menu’; Menu item is custom post type in WP. They are stored in wp_posts … Read more