Posted By

villagecreative on 10/08/10


Tagged

menu navigation active


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

gavatron
jplr


Nav with class="active" based on page


 / Published in: PHP
 

This is a way to have a navigation menu with active classes which are set according to what page is loaded. This only works on .php pages.

  1. // this goes at the top of the document, before the DOCTYPE
  2. <?php $page = basename($_SERVER['SCRIPT_NAME']); ?>
  3.  
  4.  
  5. // This goes in the page, either hard-coded, or by an include
  6. <ul id="nav">
  7.  
  8. <li <?php if ($page == 'index.php') { ?>class="active"<?php } ?>><a href="index.php" title="Welcome page">Home</a></li>
  9.  
  10. <li <?php if ($page == 'about.php') { ?>class="active"<?php } ?>><a href="about.php" title="About us page">About</a></li>
  11.  
  12. <li <?php if ($page == 'services.php') { ?>class="active"<?php } ?>><a href="services.php" title="Services page">Services</a></li>
  13.  
  14. <li <?php if ($page == 'news.php') { ?>class="active"<?php } ?>><a href="news.php" title="News page">News</a></li>
  15.  
  16. <li <?php if ($page == 'testimonials.php') { ?>class="active"<?php } ?>><a href="testimonials.php" title="Testimonials page">Testimonials</a></li>
  17.  
  18. <li <?php if ($page == 'contact_us.php') { ?>class="active"<?php } ?>><a href="contact_us.php" title="Contact us page">Contact us</a></li>
  19.  
  20. </ul>

Report this snippet  

You need to login to post a comment.