2

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.org/plugins/custom-menu/

https://wordpress.org/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.

  • 2
    No, WordPress doesn't do that by itself. Yes, you can create a function to do that. – janh Feb 1 '18 at 19:53
  • I just edited my question to insert a code – JPashs Feb 2 '18 at 8:07
7

That 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"]
| improve this answer | |

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.