Solved

How to load a different menu based on current page in wordpress?

Posted on 2015-01-29
2
176 Views
Last Modified: 2015-02-18
I have inherited a site from a new client who has a wordpress site that was heavily hand-coded by their previous webdev. The current code (in the header.php file) is:

//When page loads...
    $(".tab").hide(); //Hide all content
//Find URL location
var idToShow = location.pathname.split("/")[1];
//If URL location is one of these, then show its proper menu. If it isn't just show the home's menu.
if (idToShow == "unknown" || idToShow == "home" || idToShow == "parish" || idToShow == "formation" || idToShow == "school" || idToShow == "youth-ministry" || idToShow == "councils" || idToShow == "committees" || idToShow == "organizationsactivites" || idToShow == "contact") {
    $("#nav li."+idToShow).addClass("current").show(); //Activate first tab
    $("#"+idToShow).show();
}
else {
    $("#nav li.home").addClass("current").show(); //Activate first tab
    $("#home").show();
}

Open in new window


I am in the process of moving this site to a new host, and this works on the old host, but not the new host. I'm not sure where I could look for error logs (if there are any) or any other information that would help me solve this. Any ideas? Thanks!
0
Comment
Question by:Harrison Walker
2 Comments
 
LVL 25

Expert Comment

by:James Rodgers
ID: 40577321
this doesn't load a new menu,  all the menus are loaded  this hides   them all then shows one based on a  page name in the path.

unless you missed a file when migrating the code or the permalinks are not set properly, most likely, this should work

http://codex.wordpress.org/Using_Permalinks

see if that helps
0
 
LVL 3

Accepted Solution

by:
Morgan Finley earned 500 total points
ID: 40578321
There's a much better way to do this than loading all the menus, hiding them and then showing one based on a page name in the path. It's best that you use a conditional statement to load different menus. Since you're doing this in the header.php file, the code might look something like this:

if (is_page( 'Some Page Title' )) {
<div class="menu1">
  <?php wp_nav_menu('2'); ?>
</div>
} else {
<div class="menu1">
  <?php wp_nav_menu(); ?>
</div>
}

Open in new window


My example loads an alternate menu with the ID of 2, if it's the page titled "Some Page Title", and if it's not that page the default menu will be loaded. You can put just about anything within that conditional loop, and you can even string multiple conditionals together with the && (AND) and || (OR) logic operators.

You can learn about conditional logic at http://codex.wordpress.org/Conditional_Tags
You can learn about calling and modifying menus at http://codex.wordpress.org/Function_Reference/wp_nav_menu
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

821 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