Use php to highlight the current page


/ Published in: PHP
Save to your folder(s)

What this does is takes the name of your document using the basename function. And then you echo out the current id (id="current") if this page is present. Then using CSS we can give the current id an active color.


Copy this code and paste it in your HTML
  1. <?php $currentPage = basename($_SERVER['SCRIPT_NAME']); ?>
  2. <div id="nav">
  3.  
  4. <ul>
  5. <li><a href="index.php"<?php if($currentPage == 'index.php') { echo 'id="current"'; } ?>>home</a></li>
  6.  
  7. <li><a href="about.php" <?php if($currentPage == 'about.php') { echo 'id="current"' ;} ?>>About us</a></li>
  8.  
  9. <li><a href="portfolio.php" <?php if($currentPage == 'portfolio.php') { echo 'id="current"'; } ?>>Portfolio</a></li>
  10.  
  11. <li><a href="contact.php" <?php if($currentPage == 'contact.php') { echo 'id="current"'; } ?>>contact</a></li>
  12. </ul>
  13.  
  14. </div><!-- END nav -->

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.