PHP

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

Basant

Recent Posts

AWS

Steps: AWS puttyubuntu sudo su apt update apt install apache2 service apache2 start service apache2…

1 day ago

How to Center submit button of wpForm

You must navigate to Settings » General first. Add wpf-center to the Form CSS Class…

8 months ago

How to add Marquee on any website wordpress,PHP,HTML,eBlogger ?

if you want to add marquee on any website (wordpress,PHP,HTML,Blogger). you just need to follow…

8 months ago

How to Autoplay and Loop YouTube Videos in WordPress

First Method: Use below code to play youtube video in loop and most important is…

1 year ago