• Skip to primary navigation
  • Skip to main content
  • Skip to footer
  • Books
    • Content Strategy for WordPress (2015)
    • WordPress for Web Developers (2013)
    • Beginning WordPress 3 (2010)
  • Blog
    • Content Modeling for WordPress
    • WordPress Hidden Gems
    • Web Design
  • Work
    • Presentations and Interviews
    • on GitHub →
    • MLIS Class Projects (2019-2022)
    • Portfolio (2002-2019)
    • WordPress Plugins

Stephanie Leary

Writer, Front End Developer, former WordPress consultant

  • About
    • Press Kit
    • Presentations and Interviews
  • Contact Me

Call a navigation menu using a shortcode

July 13, 2010 Stephanie Leary 52 Comments

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.

WordPress Navigation, shortcodes

This is an excerpt from Content Strategy for WordPress.My latest books are Content Strategy for WordPress (2015) and WordPress for Web Developers (2013). Sign up to be notified when I have a new book for you.

Reader Interactions

Comments

  1. Hakan says

    September 16, 2010 at 6:05 pm

    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

    Reply
    • Jesus says

      January 16, 2015 at 12:02 pm

      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.

      Reply
      • Hamed says

        October 10, 2016 at 3:29 am

        after searching everywhere finally this saved my problem. I appreciate it as it works perfectly fine.

        Reply
      • Sarah says

        July 28, 2017 at 5:40 am

        Thank You, my class isn’t displaying… tried styling the menu, I need help please

        Reply
  2. Jennifer says

    December 6, 2010 at 11:40 am

    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.

    Reply
  3. Pascal says

    January 15, 2011 at 10:24 am

    Thanks for this post, it helped me a lot !

    Reply
  4. Heiko says

    March 24, 2011 at 11:44 am

    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!

    Reply
  5. Marfalkov says

    April 15, 2011 at 8:54 pm

    Great!

    Thank You

    Reply
  6. JN says

    May 4, 2011 at 8:33 pm

    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!

    Reply
  7. Matt Graham says

    June 20, 2011 at 2:01 pm

    Awesome. Used this in conjunction with a SlideDeck for front page nav. Stellar.

    Thanks!

    Reply
  8. Jennifer Carello says

    June 20, 2011 at 2:34 pm

    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.

    Reply
  9. JN says

    June 30, 2011 at 9:22 am

    Now need a slight edit – any thoughts?

    Need the ability to display just the sub pages from the page it displays on?

    Reply
    • Stephanie says

      June 30, 2011 at 10:14 am

      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.

      Reply
  10. Thomas Bacon says

    March 15, 2012 at 3:30 pm

    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!

    Reply
  11. Stephan says

    May 13, 2012 at 4:33 am

    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?

    Reply
  12. vasch says

    May 15, 2012 at 4:01 am

    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!

    Reply
  13. Reise-Dealz.de says

    July 27, 2012 at 4:19 am

    Thank you, worked brilliant for me! Is there a way to integrate some Pictures in that menu? That would be awesome!

    Reply
  14. Andras says

    September 27, 2012 at 3:12 pm

    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!

    Reply
  15. Andras says

    September 28, 2012 at 5:02 am

    Can’t post the full code any idea why? If the admin can edit my posts I1d like to get rid of my mess :)

    Reply
    • Stephanie Leary says

      October 8, 2012 at 4:52 pm

      Sorry about that, Andras. You can post PHP by surrounding it in a code shortcode with PHP as the lang attribute: [[code lang=”PHP”]]

      Reply
    • Stephanie Leary says

      October 8, 2012 at 4:53 pm

      … Or post a link to a pastebin or gist.

      Reply
  16. chrismccoy says

    October 31, 2012 at 8:35 am

    very nice sillybean ;)

    Reply
  17. Arthur says

    November 12, 2012 at 6:47 am

    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

    Reply
  18. Kim says

    February 26, 2013 at 9:35 pm

    Works great, thank you for posting!

    Reply
  19. GodinezB says

    February 28, 2013 at 12:19 pm

    Just thank you, ..just the code I was looking for :3

    Reply
  20. Urlaubstracker says

    March 5, 2013 at 5:08 am

    Perfect! Thank you so much for this work! You save me lot of time.

    Reply
  21. Andrew says

    March 12, 2014 at 4:21 am

    This worked a treat, thank you so much!

    Reply
    • Steve says

      January 7, 2016 at 12:48 pm

      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??

      Reply
  22. Mathias Fege says

    April 8, 2015 at 5:38 pm

    Great! It works: http://www.enerstable.eu

    Thanks!

    Reply
  23. Purohit R says

    April 29, 2015 at 1:48 am

    Thanks a lot for solution..its works perfect..you have made my day :)

    Reply
  24. shane says

    April 30, 2015 at 10:18 am

    Great fix!!! I LOVE the simplicity of this, thanks.

    Reply
  25. Lucky Bhumkar says

    September 30, 2015 at 4:32 am

    Very helpful. Thanks for sharing..!

    Reply
  26. Sean Vandenberg says

    September 30, 2015 at 3:35 pm

    Gracias! :) – Sean

    Reply
  27. Eleazar Madera says

    November 25, 2015 at 2:15 pm

    I found this 5 years later the release… and it works Amazingly!!! THANK YOU!!!

    Reply
  28. Dimas says

    January 19, 2016 at 1:20 pm

    fantastic, thanks for the help. this what I really need

    Reply
  29. Panpic says

    March 26, 2016 at 2:45 am

    Thank you very much
    Anyway could you help me; How do I parse menu with toggle sub menu item ?

    Reply
  30. Anh Tran says

    September 8, 2016 at 11:11 pm

    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!

    Reply
  31. Austin says

    November 21, 2016 at 6:59 pm

    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.

    Reply
  32. mcris says

    January 23, 2017 at 10:27 am

    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?

    Reply
  33. Rolf Service says

    March 8, 2017 at 8:07 am

    Still usefull in 2017.
    Thx :)

    Reply
  34. Alex Williams says

    November 15, 2017 at 3:44 pm

    This actually worked. thanx!

    Reply
  35. Steven says

    November 27, 2017 at 3:43 am

    Inserting tis code in functions.php broke my website immediately, is there any way of doing this differently?

    Asking for a friend!

    Reply
    • Anthony says

      November 22, 2022 at 10:25 am

      I have the same problem. Did you ever solve this?

      Reply
  36. Jenn says

    December 31, 2021 at 6:38 pm

    Thank you for this! Even 11 years later, your post is still relevant. :)

    Reply
  37. Ben says

    October 1, 2022 at 10:10 am

    Just what I needed, thank you!

    Reply
  38. Anthony says

    November 22, 2022 at 10:26 am

    Inserting this code in functions.php broke my website immediately. Am I doing something wrong? Can anyone help?

    Reply

Leave a Reply to Andras Cancel reply

Your email address will not be published. Required fields are marked *

Footer

My Books

I’m a front end developer at Equinox OLI, working on open source library software. I was previously a freelance WordPress developer in higher education. You can get in touch here or on LinkedIn.

Copyright © 2025 Stephanie Leary · Contact