?
Solved

Get menu name from parent page

Posted on 2013-11-20
4
Medium Priority
?
482 Views
Last Modified: 2013-11-21
I'd like to keep the amount of page templates to a minimum in a theme I am creating but would like to add the ability to have secondary menus on a page based on which page the user is on.

For instance, we'll always have the primary navigation at the top of the page.

But I'd like to build separate menus that the user can custom create in the appearance >> menus area.  

I'd then like to add some dynamic code in my page.php template that determines which parent page we are on, then displays any extra navigation if there is one or goes to a default otherwise.  

I'm looking for some pointers on how to select the secondary navigation menus because I am not adding them into a location in the menus area.  Mainly, needing to create a variable for the menu name, then use that variable to select a menu with wp_nav_menu();
0
Comment
Question by:axessJosh
[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
  • 2
4 Comments
 
LVL 17

Expert Comment

by:OmniUnlimited
ID: 39665614
I would think that all you'd need to do is set up a switch statement in your template based off the page id:

<?php
global $wp_query;

switch ($wp_query->post->ID) {
     case 127:
           $theme_location = 'sidebar_menu';
           $menu = 'boss_sidebar_menu';
           break;
     case 142:
           $theme_location = 'top_menu';
           $menu = 'custom_top_menu';
           break;
...etc.
}

$defaults = array(
	'theme_location'  => $theme_location,
	'menu'            => $menu,
	'container'       => 'div',
	'container_class' => '',
	'container_id'    => '',
	'menu_class'      => 'menu',
	'menu_id'         => '',
	'echo'            => true,
	'fallback_cb'     => 'wp_page_menu',
	'before'          => '',
	'after'           => '',
	'link_before'     => '',
	'link_after'      => '',
	'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
	'depth'           => 0,
	'walker'          => ''
);

wp_nav_menu( $defaults );
?>

Open in new window

0
 
LVL 17

Accepted Solution

by:
jrm213jrm213 earned 2000 total points
ID: 39666299
OmniUnlimited is on the right track and what he posted will work, but this kind of setup get's tedious to continue updating as you add pages or need to make changes, especially if you have other users who are not used to editing code working with the site.

I would recommend instead adding a custom field that takes the name of the secondary menu that you want to display as it's value. Read that custom field and if it is there, use it to display the menu.

This way any editor could choose to add or remove a secondary menu for a page, and even create the menu and just use it's name in that custom field, and they wouldn't have to edit any code.
0
 
LVL 2

Author Comment

by:axessJosh
ID: 39666330
Thanks JRM,

I was trying to avoid using custom fields but you are correct, that is probably the simplest way to handle it and not have to deal with getting into the code with future updates.

This may need a second question, but any thoughts on how to display the menu title with the menu?
0
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 39666380
Thanks for the points, I would have split the points between me and omniunlimited though.

You could try this for getting the title:

http://wordpress.org/support/topic/display-title-attribute-in-menu
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Transferring a wordpress site from a host or local dev server to another host can be a pain. So I've included my steps on how I have accomplished this task. Steps include an assumption that you have Cpanel access or Ftp access.. If you do not hav…
In Part I (http://www.experts-exchange.com/Web_Development/Blogs/WordPress/A_8410-Getting-Started-In-WordPress-Part-I.html), I introduced you to the powerful WordPress backend, the WordPress administrative Dashboard.  In Part II, I will introduce yo…
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…
Suggested Courses
Course of the Month11 days, 3 hours left to enroll

770 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