Reputation: 25
Could somebody point me in the right direction of how to dynamically populate the menu ID
by use of PHP or JS?.
My current code for a menu item is:
<li>
<a href="contact-us.php" class="mainlevel-nav" id="active_menu-nav" name="active_menu-nav">Contact us</a>
</li>
the id="active_menu-nav"
shows the active menu item in different color by CSS.
My problem is, whenever I have to make a change to the menu, I have to edit every page of the site, where as other parts of the site, like the footer, are all done by PHP includes as its the same on every page.
So is there a way the id="active_menu-nav"
can be populated automatically, maybe by use of the URL? For example:
if URL is example.com/conctact-us.php then id="active_menu-nav" else id=""
Hope that makes sense, my end result is the have the menu code in one file that can the be included on each page of the site.
Upvotes: 1
Views: 125
Reputation: 474
If you're trying to format a menu to be active/normal, you should assign it a class, that way it can always be uniquely identified by the ID and if you want, you can use jQuery's hasClass() to see if it's selected or not (if you have any Javascript that would require that).
To tell if the URL is "contact-us.php" you can look at $_SERVER[REQUEST_URI] and in each id or class (whatever you end up using) you can use (not tested):
class="<?php echo ($_SERVER[REQUEST_URI] == '/contact-us.php'? 'active_menu-nav':''); ?>"
This way each link can check if you've navigated to that page, and if you have, it'll set the class to the active_menu-nav class and apply the appropriate CSS rules.
However, you should use the nav bar as an include onto the site just like your footer and header so that you don't have to go through every page and change everything.
Upvotes: 3