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.
Hakan says
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
Jesus says
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.
Hamed says
after searching everywhere finally this saved my problem. I appreciate it as it works perfectly fine.
Sarah says
Thank You, my class isn’t displaying… tried styling the menu, I need help please
Jennifer says
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.
Pascal says
Thanks for this post, it helped me a lot !
Heiko says
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!
Marfalkov says
Great!
Thank You
JN says
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!
Matt Graham says
Awesome. Used this in conjunction with a SlideDeck for front page nav. Stellar.
Thanks!
Jennifer Carello says
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.
JN says
Now need a slight edit – any thoughts?
Need the ability to display just the sub pages from the page it displays on?
Stephanie says
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.
Thomas Bacon says
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!
Stephan says
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?
vasch says
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!
Reise-Dealz.de says
Thank you, worked brilliant for me! Is there a way to integrate some Pictures in that menu? That would be awesome!
Andras says
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!
Andras says
Can’t post the full code any idea why? If the admin can edit my posts I1d like to get rid of my mess :)
Stephanie Leary says
Sorry about that, Andras. You can post PHP by surrounding it in a code shortcode with PHP as the lang attribute: [[code lang=”PHP”]]
Stephanie Leary says
… Or post a link to a pastebin or gist.
chrismccoy says
very nice sillybean ;)
Arthur says
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
Kim says
Works great, thank you for posting!
GodinezB says
Just thank you, ..just the code I was looking for :3
Urlaubstracker says
Perfect! Thank you so much for this work! You save me lot of time.
Andrew says
This worked a treat, thank you so much!
Steve says
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??
Mathias Fege says
Great! It works: http://www.enerstable.eu
Thanks!
Purohit R says
Thanks a lot for solution..its works perfect..you have made my day :)
shane says
Great fix!!! I LOVE the simplicity of this, thanks.
Lucky Bhumkar says
Very helpful. Thanks for sharing..!
Sean Vandenberg says
Gracias! :) – Sean
Eleazar Madera says
I found this 5 years later the release… and it works Amazingly!!! THANK YOU!!!
Dimas says
fantastic, thanks for the help. this what I really need
Panpic says
Thank you very much
Anyway could you help me; How do I parse menu with toggle sub menu item ?
Anh Tran says
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!
Austin says
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.
mcris says
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?
Rolf Service says
Still usefull in 2017.
Thx :)
Alex Williams says
This actually worked. thanx!
Steven says
Inserting tis code in functions.php broke my website immediately, is there any way of doing this differently?
Asking for a friend!
Anthony says
I have the same problem. Did you ever solve this?
Jenn says
Thank you for this! Even 11 years later, your post is still relevant. :)
Ben says
Just what I needed, thank you!
Anthony says
Inserting this code in functions.php broke my website immediately. Am I doing something wrong? Can anyone help?