Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2015-01-29
2
Medium Priority
?
197 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
Suggested Courses

886 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