Display WordPress menus anywhere you want

After WordPress reached version 3.0 many things became easier to use. Among different new features, creating a navigational menu became accessible to anyone and today everyone can build their own menus with a few simple clicks of a mouse button.

While creating menus where you can easily link any post, page, category or any external link is a piece of cake, showing the menu on different places along your site can be a little bit bigger bite for those who aren’t into WordPress development.

Depending on the theme you’re using, you can easily display a newly created menu on top of your page where we’re all used to find navigation. You may have the option to place your menu on the sidebar or even right down in the footer area, but what if you wanted to show the menu in your posts, pages or even in the widget?

No, you don’t have to create custom lists or retype the entire HTML code in order to replicate the links; you can take the advantage of shortcodes and place an existing menu anywhere you want!

In the following lines, we’re about to show you how to do that and we’re sure that you’re going to like your new ‘menu in the shortcode’ feature.

Create a shortcode and display WordPress menus anywhere you want:

  1. Open functions.php file
  2. Copy and paste the code which will register the shortcode:
  3. function menu_function($atts, $content = null) {
    extract(
    shortcode_atts(
    array( 'name' => null, ),
    $atts
    )
    );
    return wp_nav_menu(
    array(
    'menu' => $name,
    'echo' => false
    )
    );
    }
    add_shortcode('menu', 'menu_function');
    
  4. Save changes

Right after you save the changes, you will be able to display WordPress menus anywhere you want. All you have to do is to write down the shortcode where you have to specify the name of menu you’re going to use:

[menu name=”main-menu”]

In most cases, you main menu will have the “main-menu” name attached to itself. But if you’re going to use any other menu, simply enter its name in the shortcode. You don’t have to know every menu name by heart; instead, navigate to Appearance -> Menus and browse through the list of menus or create new ones.

Since you’re now able to display WordPress menus practically anywhere, you can create numerous navigations which you’re going to use in your WordPress powered website. You can have one in a post, another one in template files, a Text Widget, etc.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

One thought on “Display WordPress menus anywhere you want

Leave a Reply

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