Solved

WordPress - how to display custom menu in a theme?

Posted on 2011-02-18
8
1,209 Views
Last Modified: 2012-05-11
Hi,
I'm using WordPress 3.0.5, I added a menu called "Top Menu" through WP-Admin-> Appearance->Menus
I added some pages to it, how can I display it in the header of my theme? Is there any function to call such custom menus?

Thanks
0
Comment
Question by:rajoo_sharma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 34928435
If the WP Nav Menu is active in your current theme, just add this to your header.php file where you want the menu:
<?php wp_nav_menu( array( 'sort_column' => 'menu_order', 'container_class' => 'menu-header' ) ); ?>

Open in new window


If it isn't activated try adding this just before the closing ?> PHP tag in the functions.php file along with the code above for the header.php:
add_theme_support( 'menus' );

Open in new window

0
 

Author Comment

by:rajoo_sharma
ID: 34931894
Hi,
Thanks for the reply. I'm new to WordPress so my queries could be really stupid :)

1. I've two custom menus "Top Menu" and "Bottom Menu"
2. I added the following code in the header.php
<?php wp_nav_menu( array( 'sort_column' => 'menu_order', 'container_class' => 'menu-header' ) ); ?>

Open in new window

3. It always shows me the Top Menu, how can I get a menu by name or ID?
4. Adding or removing this piece of code
add_theme_support( 'menus' );

Open in new window

at the end of the functions.php makes no difference. The menu always appears.

Regards
0
 
LVL 23

Accepted Solution

by:
jeremyjared74 earned 500 total points
ID: 34931955
I am not sure what you have going on in your menu's. Beside the spot where you add a custom menu it should say something like your theme supports 1 or 2 custom menu's or your theme doesn't currently support custom menus. If it says it supports 2 then you will have to find where the call to the nav menu is.

If your theme doesn't support custom menu's then add this code for up to three custom menu's:

CODE FOR FUNCTIONS.php:
add_action( 'init', 'register_my_menus' );

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

Open in new window


Then add this to the header:
<?php wp_nav_menu( array( 'theme_location' => 'primary-menu' ) ); ?>

Open in new window


And this to the footer.php or wherever you want it:
<?php wp_nav_menu( array( 'theme_location' => 'secondary-menu' ) ); ?>

Open in new window


Then use this one in the sidebar.php for a sidebar menu:
<?php wp_nav_menu( array( 'theme_location' => 'tertiary-menu' ) ); ?>

Open in new window

0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

Author Comment

by:rajoo_sharma
ID: 34932007
Hi,

Thanks for such a great detailed explanation. I could achieve what I wanted.

I've one more query related to displaying select Page's/Post's teaser in a block using admin. How can I accomplish this? e.g. a video on a home page or teaser of about us page on home page in a block / region.

Should I post another question for this?

Regards
0
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 34932147
You could use the featured image feature to achieve this. Here is a link to a good source. Let me know if you have any questions.

http://ponderwell.net/2011/02/wordpress-featured-images-and-custom-thumbnail-sizes/
0
 

Author Comment

by:rajoo_sharma
ID: 34932244
Thanks again,
I'll surely post questions as I've just started using WordPress :).

Regards
0
 

Author Closing Comment

by:rajoo_sharma
ID: 34932269
Thank you for such an accurate solution.
0
 
LVL 23

Expert Comment

by:jeremyjared74
ID: 34932297
You're welcome. Let me know if you run into any problems with the featured image hack.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question