admin管理员组文章数量:1318958
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;
}
}
}
本文标签: theme developmentFunctions php shortcode for displaying main menu with no child items
版权声明:本文标题:theme development - Functions php shortcode for displaying main menu with no child items 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742057566a2418387.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论