0

I have the following simple functions.php snippet to generate a shortcode which will add a menu to a Wordpress site. I would like to do two new things so that I can use this in a footer. I'm lazy and don't want to create a footer menu ;)

I need to:

  1. Filter off the Homepage
  2. Show only the top-level parent items and no child pages
// Show Footer Menu
function print_menu_shortcode($atts, $content = null) {
extract(shortcode_atts(array( 
                             'name' => null, 
                            'class' => null ), $atts));
return wp_nav_menu( array( 
                         'menu' => $name, 
                         'menu_class' => 'footer-menu', 
                         'echo' => false ) );
}

add_shortcode('menu', 'print_menu_shortcode');
1

You could set the 'depth' argument to 1 in your wp_nav_menu() call to get only top level items, along with the custom menu walker, something like this:

return wp_nav_menu( array( 'menu' => $name, 'menu_class' => 'footer-menu', 'echo' => false, 'depth' => 1, 'walker' => new custom_footer_menu_walker ) );

Add the custom menu walker to your functions.php (12345 is the ID of your homepage, that should be excluded):

class custom_footer_menu_walker extends Walker_Nav_Menu {

    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {

        parent::start_el($item_html, $item, $depth, $args);

        $exclude = array();
        $exclude[] = 12345;

        if ( ! in_array( $item->object_id, $exclude ) ) {
            $output .= $item_html;
        }
    }
}
2
  • OK, that looks good but how do I also create all of this as a shortcode? Oct 18 '20 at 23:07
  • You already did. Just replace your return statement with mine, in your print_menu_shortcode function. Then you can call the [menu] shortcode wherever you need to. Oct 19 '20 at 7:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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