WordPress Custom Menus

Recently introduced in WordPress 3 is the ability to create custom menus.  For anyone who has ever developed a theme, this was long a overdue feature.  In this tutorial we are going to look at the new menu system and how to implement it.

We are going to be using our site as an example for this tutorial.  As you can see from the picture below, we have two custom menus built into our site.  The top menu we will refer to as the “Primary Menu” and the bottom menu we will refer to as the “Secondary Menu”.

Add the menu function

The first thing we need to do is open up your themes functions.php file and add the following code:

add_action( 'init', 'my_custom_menus' );

function my_custom_menus() {
	register_nav_menus(
		array(
			'primary-menu' => __( 'Primary Menu' ),
			'secondary-menu' => __( 'Secondary Menu' )
		)
	);
}

This code creates a new function “my_custom_menus”, which you can name whatever you want. Within this function, we call “register_nav_menus” which takes two arguments, the slug which is called from within our code and the label which you will be used in the WordPress menu manager. As you can see, we have defined both the Primary and Secondary menus. That’s it for the functions.php file, so you can save and close it now.

Modifying the template

Next we want to open up the template file that will contain our navigation.  For our example, that is the header.php file.  After we locate the spot we want our primary menu to be displayed at, we paste in this code:

<?php wp_nav_menu( array( 'theme_location' => 'primary-menu', 'menu_class' => 'primary', 'fallback_cb' => '') ); ?>

and then we do the same for the secondary menu at it’s location in the document:

<?php wp_nav_menu( array( 'theme_location' => 'secondary-menu', 'menu_class' => 'secondary', 'fallback_cb' => '') ); ?>

As you can see, we define a theme_location which corresponds to the appropriate menu.   We then define a menu_class, which will be the CSS class used for the unordered list the menu uses.   We are also leaving fallback_cb blank, so if a menu doesn’t exist, nothing is displayed.

Take a look at the WordPress Codex for a complete list of the parameters you can use.

Creating your menu

Now that we have taken care of the code portion of this tutorial, all that’s left is to create our menu.  WordPress’s new menu system is very easy to use and allows you to combine pages and categories to create any menu you want.

From your WP admin Dashboard, go to Appearances > Menus and you will see the menu builder.  The first thing you need to do is set your new menus in the “Theme Locations” box in the upper left hand corner.

Next you will want to select which menu you would like to create first.

You can then start adding Pages or Categories to your menu.  Once added, you can re-arrange and create sub-menus by simply dragging and dropping the menu items.

Don’t forget to click save after you have created your new menu!

S

h

a

r

e

  • Share this post on Delicious
  • StumbleUpon this post
  • Share this post on Digg
  • Tweet about this post
  • Share this post on Mixx
  • Share this post on Technorati
  • Share this post on Facebook
  • Share this post on NewsVine
  • Share this post on Reddit
  • Share this post on Google
  • Share this post on LinkedIn

About the author

Nate Smith has written 22 articles for Box Model Junkie

I am a 28 year old designer / developer from Peoria, IL who is very into PHP, WordPress, Django and jQuery. I have a huge passion for the web and love learning new things. You can follow me on twitter @imns81

9 Responses to "WordPress Custom Menus"

  • Tweets that mention... 08:55 PM 15/7/2010

    [...] This post was mentioned on Twitter by Olof, Box Model Junkie. Box Model Junkie said: WordPress Custom Menus [...]

  • FAQPAL 01:28 PM 20/7/2010

    Thanks for the share.

  • Marcell Purham | Webdevtuts 03:53 PM 20/7/2010

    Great tutorial. WordPress is starting to give designers more control over different functional

    • Box Model Junkie 05:29 AM 29/7/2010

      Agreed, the new menu system is great because you don’t have to edit any code to get it working (assuming you theme has it setup already for you). Like you said, this is a really great feature for designers.

  • Wilmer 01:41 PM 21/7/2010

    Thank you for this tutorial!
    There is a ‘-sign missing on the left of my_custom_menus. It gave me a white screen.

    • Box Model Junkie 04:27 PM 22/7/2010

      Wilmer,

      Glad you liked it and thanks for catching that. I’ve updated the code in the article.

  • Nichive 01:17 AM 16/4/2012

    Thanks for the functions, that helped me.
    Can I have a simple CSS to activate accordion type menu, please?

  • Trevor 11:39 PM 20/6/2012

    Damn this really helped me. I needed to have custom menus!!! Although, I agree some sort of css example would be really benefictal. I can’t seem to edit the ul and li or a of the .primary menu class. Does this not organize the pages in lists? What am I doing wrong?

    /*** Menu ***/
    .main-menu { clear:both; height:34px; padding:0px 56px 0px 0px;}

    .primary { float:left; padding:30px 0px 0px 0px;margin:0px 0px 0px 250px;list-style:none;}

    //***Nothing below here works :( ***//
    .primary ul { list-style:none; overflow:hidden;}
    .primary ul li { float:right; overflow:hidden; padding:15px 36px 0px 36px; background:url(images/ver.png) no-repeat 0 16px; margin:0px 0px 0px -2px;}

    .primary ul li a { display:block; float:left; display:block; height:100%; overflow:hidden; cursor: pointer; text-decoration:none; text-shadow:0 0 3px #000000;}
    .primary ul li a { font-size:2em; color:#fff;}
    .primary ul li a:hover { color:#ff0895;}

    .primary ul li.current_page_item a {color:#ff0895;}

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

About

Box Model Junkie is a site addicted to Front-end / UI development. We focus on tutorial relating to JavaScript frameworks, CSS3 and HTML5.