I am making a menu on a Wordpress template and need the menu to detect the current page and highlight it. So for instance, my menu is:
<li><a href="index.php" class="current">Home</a></li>
<li><a href="about.php">About</a></li>
So if the user is on the About page, I want that one to have the "class=current". How is this possible? I hear using the $_SERVER['PHP_SELF']
is possible? Please appreciate that I have zero knowledge in php coding, so kindly make any replies detailed.
I use a little snippet like this one. If your visitor is on about.php
, $basename
will equal about
.
$basename = substr(strtolower(basename($_SERVER['PHP_SELF'])),0,strlen(basename($_SERVER['PHP_SELF']))-4);
The HTML Would be something like this.
<li><a href="index.php"<?php if ($basename == 'index') echo ' class="current"'; ?>>Home</a></li>
<li><a href="about.php"<?php if ($basename == 'about') echo ' class="current"'; ?>>About</a></li>
$myPage = $_SERVER['PHP_SELF'];
<li><a href="index.php" class="<?echo ($myPage == 'index.php'?'current':'')?>">Home</a></li>
<li><a href="about.php" class="<?echo ($myPage == 'about.php'?'current':'')?>">About</a></li>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With