Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now