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:
- Filter off the Homepage
- 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');
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:
- Filter off the Homepage
- 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');
Share
Improve this question
edited Oct 16, 2020 at 8:31
Lenin
1902 silver badges14 bronze badges
asked Oct 16, 2020 at 2:01
user3998694user3998694
11 bronze badge
1 Answer
Reset to default 1You 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;
}
}
}