Today I came across a weird situation: I needed to place a navigation menu in the content of a page. A shortcode was the obvious solution, but there doesn’t appear to be one built in for menus. I created this one very quickly:
function print_menu_shortcode($atts, $content = null) { extract(shortcode_atts(array( 'name' => null, ), $atts)); return wp_nav_menu( array( 'menu' => $name, 'echo' => false ) ); } add_shortcode('menu', 'print_menu_shortcode');
Place this in functions.php
, then use [menu name="main-menu"]
to call the menu in your content (replacing "main-menu"
with your menu’s slug, of course).
You could adapt this to accept any of the other arguments available for wp_nav_menu()
, but this served my purposes.
Thanks for the post. I wrote it like this:
function my-shortcode-name() {
return wp_nav_menu( array( ‘theme_location’ => ‘mymenu-name’,’depth’ => ‘2’,’menu_class’ => ‘my-class-name’, ‘echo’ => false ) );
;}add_shortcode(‘my-shortcode-name’,’my-shortcode-name’);
One problem tough. When I put the shortcode in a custom post type field the class “current_page_item” wont show up as it should. Any solution for this?
I tested “wp_reset_query();”:
function my-shortcode-name() {
wp_reset_query();
return wp_nav_menu( array( ‘theme_location’ => ‘mymenu-name’,’depth’ => ‘2’,’menu_class’ => ‘my-class-name’, ‘echo’ => false ) );
;}add_shortcode(‘my-shortcode-name’,’my-shortcode-name’);
It works on pages but mess up the site on posts.
Any solution?
BR.
Hakan
Hehehe almost 5 years later, but I didn’t need it till today. Perhaps you were looking something like this:
In functions.php
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 in the content
[menu name="-your menu name-" class="-your class-"]
Hope you find your solution in time.
after searching everywhere finally this saved my problem. I appreciate it as it works perfectly fine.
Thank You, my class isn’t displaying… tried styling the menu, I need help please
Thnak you!! This is brilliant! I am not a coder but this was exactly what I needed. I saw some popup somewhere to donate but don’t now. I will throw in a few bucks for this.
Thanks for this post, it helped me a lot !
That’s great! I searched now for while to solve that problem because I did too many widgets with widget logic and now this works out of the box – copy&paste and done – thanks a lot!
Great!
Thank You
Thank you!
I added this to our customer knowledgebase as well:
http://www.anysitesupport.com/how-do-i-add-a-wordpress-menu-to-the-content-of-my-page/
And linked back to here for reference. This really saved one of our customers – thanks again!
Awesome. Used this in conjunction with a SlideDeck for front page nav. Stellar.
Thanks!
Matt, LOVE SLIDEDECK! I just checked it out from your post and it is so great! I am definitely purchasing it Thanks for that tip.
Now need a slight edit – any thoughts?
Need the ability to display just the sub pages from the page it displays on?
Hmm, tricky. If you can use a plain old page list instead of the menu, I can do that! Here’s the code for a theme file, a functions.php filter, or a shortcode.
I was about to write this same code, then I thought I bet someone else has had this same exact issue so I searched, and sure enough… This worked great, thanks!
Thanks for this post! Your description is – for me – as a WP-newbie inconceivable, as I do not understand where to copy the text “[menu name =” main menu “]” to? Also in the functions.php of the theme below the copied function?
For hours I’ve been looking to get this one!
THANKS! It works smoothly.
If anyone can make this work with a combobox or dropdown, it would even be better, but this one is fine already!
Thank you, worked brilliant for me! Is there a way to integrate some Pictures in that menu? That would be awesome!
Hi,
I’m looking for a solution to replace the default wordpress menu.
My “custom” menu generated with the ‘Accordion Pro’ plug-in will be perfect.
It outputs a shortcode like this:
[accordion_pro id=’5′]
Currently the header.php file has the following code to make the default menu appear:
‘primary’ ) ); ?>
Where do I have to place the shortcode to replace the default menu with the Accordion Pro menu?
Thanks!
Can’t post the full code any idea why? If the admin can edit my posts I1d like to get rid of my mess :)
Sorry about that, Andras. You can post PHP by surrounding it in a code shortcode with PHP as the lang attribute: [[code lang=”PHP”]]
… Or post a link to a pastebin or gist.
very nice sillybean ;)
Hi Stephanie,
thx for this great post. I was searching for a plugin, but with a shortcode its much easier. At the moment I m trying to combine it with a dropdown menu. if it works, ill post here again :-)
thx for your help!
cheers Arthur
Works great, thank you for posting!
Just thank you, ..just the code I was looking for :3
Perfect! Thank you so much for this work! You save me lot of time.
This worked a treat, thank you so much!
Andrew – I know this is a long time afterward but hoping you can help me with this issue. How did you get the dropdown menu to show??
Great! It works: http://www.enerstable.eu
Thanks!
Thanks a lot for solution..its works perfect..you have made my day :)
Great fix!!! I LOVE the simplicity of this, thanks.
Very helpful. Thanks for sharing..!
Gracias! :) – Sean
I found this 5 years later the release… and it works Amazingly!!! THANK YOU!!!
fantastic, thanks for the help. this what I really need
Thank you very much
Anyway could you help me; How do I parse menu with toggle sub menu item ?
Just looking around for this thing as I need to insert a menu into a location in the theme without modifying the template files. Nice!
Wow thank you Stephanie! I have been using a stupid plugin to do this same thing for months now. I was going through the plugins on my site this weekend, trying to get rid of any unnecessary ones, which is when I found this guide. Cheers.
Hello,
This code worked but the menu is showing vertically. I tried to add a display:inline; to have it horizontal but no change. Can you help?
Still usefull in 2017.
Thx :)
This actually worked. thanx!
Inserting tis code in functions.php broke my website immediately, is there any way of doing this differently?
Asking for a friend!
I have the same problem. Did you ever solve this?
Thank you for this! Even 11 years later, your post is still relevant. :)
Just what I needed, thank you!
Inserting this code in functions.php broke my website immediately. Am I doing something wrong? Can anyone help?