active class using php In 5 Minutes

active class using php In 5 Minutes

You can simply follow steps and add active class in your navigation using PHP

Step1:  In Header page Modified navigation link as per your need

<li class=”nav-item <?php if ($phpSelf==”index.php”) { echo “active”; } ?>”><a href=”index.php” class=”nav-link”>Home</a></li>
<li class=”nav-item <?php if ($phpSelf==”about.php”) { echo “active”; } ?>”><a href=”about.php” class=”nav-link”>About</a></li>
<li class=”nav-item <?php if ($phpSelf==”menu.php”) { echo “active”; } ?>”><a href=”menu.php” class=”nav-link”>Menu</a></li>
<li class=”nav-item <?php if ($phpSelf==”blog.php”) { echo “active”; } ?>”><a href=”blog.php” class=”nav-link”>Stories</a></li>
<li class=”nav-item <?php if ($phpSelf==”contact.php”) { echo “active”; } ?>”><a href=”contact.php” class=”nav-link”>Contact</a></li>
<li class=”nav-item cta”><a href=”reservation.php” class=”nav-link”>Book a table</a></li>

Step2: Open particular page like index.php,about.php so on..

Step3: Add these code before including header.php

$phpSelf = trim(basename($_SERVER[‘PHP_SELF’]));

That’s It. Now you can see your navigation link has active class

Thanks. Basant Mallick

Secured By miniOrange