Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting page filename using php?

Tags:

php

detect

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.

like image 635
alexander Avatar asked Dec 09 '22 11:12

alexander


2 Answers

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>
like image 64
Dutchie432 Avatar answered Dec 27 '22 13:12

Dutchie432


$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>
like image 25
Jason Benson Avatar answered Dec 27 '22 15:12

Jason Benson