Solved

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

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

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

744 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

12 Experts available now in Live!

Get 1:1 Help Now