admin管理员组文章数量:1289558
I need to insert a menu in the text of one page. I found these two plugin but none of them work. Both of them haven't been updated for 6 years:
/
/
I found this code to create my own shortcode
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' => $class, 'echo' => false ) );
}
add_shortcode('menu', 'print_menu_shortcode');
And then shortcode should be:
[menu name="-your menu name-" class="-your class-"]
It works but the class is not printed at all. What is wrong in the function? I need to print the class.
I need to insert a menu in the text of one page. I found these two plugin but none of them work. Both of them haven't been updated for 6 years:
https://wordpress/plugins/custom-menu/
https://wordpress/plugins/custom-menu-shortcode/
I found this code to create my own shortcode
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' => $class, 'echo' => false ) );
}
add_shortcode('menu', 'print_menu_shortcode');
And then shortcode should be:
[menu name="-your menu name-" class="-your class-"]
It works but the class is not printed at all. What is wrong in the function? I need to print the class.
Share Improve this question edited Feb 2, 2018 at 8:10 JPashs asked Feb 1, 2018 at 18:18 JPashsJPashs 1773 gold badges3 silver badges12 bronze badges 2- 2 No, WordPress doesn't do that by itself. Yes, you can create a function to do that. – janh Commented Feb 1, 2018 at 19:53
- I just edited my question to insert a code – JPashs Commented Feb 2, 2018 at 8:07
2 Answers
Reset to default 12That code should work. Are you usign "myclass" as the class and not ".myclass"?
Is this for a specific use where class will always be the same? If you're only looking to ever use this on one place, you can do this:
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' => 'myclass', 'echo' => false ) );
}
add_shortcode('menu', 'print_menu_shortcode');
Then change the section 'menu_class' => 'myclass' with the class you need. this will avoid having to use the class. Again, don't use the "." in front of the class here.
Short code usage:
[menu name="menu_name"]
I know this years too late, but just in case anybody encounters this again, you can use this code to be able include a class with your shortcode and its return value. I modified it with the corresponding answers before in here.
function print_menu_shortcode($atts=[], $content = null) {
$shortcode_atts = shortcode_atts([ 'name' => '', 'class' => '' ], $atts);
$name = $shortcode_atts['name'];
$class = $shortcode_atts['class'];
return wp_nav_menu( array( 'menu' => $name, 'menu_class' => $class, 'echo' => false ) );
}
Shortcode:
[menu name="menu_name" class="your_class"]
I hope this helps anyone that may need it in the future!
本文标签: functionsShortcode to insert menu in page body
版权声明:本文标题:functions - Shortcode to insert menu in page body? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741478133a2381006.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论