Solved

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

Posted on 2015-01-29
2
189 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
[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 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…
The viewer will learn how to count occurrences of each item in an array.

623 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