在使用 header.php 页面时,我将如何使用 PHP 定义导航栏的 "active" 选项卡

How would I go about using PHP to define the "active" tab of a navigation bar when using a header.php page

我正在尝试为我们所在的任何页面创建一个具有 "current" class 的导航栏。但是导航栏位于 header.php 页面上,我使用包含标记将页面与 header.

绑定

我该怎么做? 我以为我会使用类似的东西;

if(strpos($_SERVER['REQUEST_URI'], 'index') !== false){;

但我没有足够的信心独自解决这个问题。

https://jsfiddle.net/zo5oy46q/

谢谢!

在每个页面的顶部创建一个名为 $page_name 的变量,并为每个页面分配一个名称。

然后在每个选项卡中,在页面名称的基础上添加 current class。

示例:

<a href="/home" class="<?php echo $page_name == 'home' ? 'current' : '' ?>"></a>
<a href="/contact" class="<?php echo $page_name == 'contact' ? 'current' : '' ?>"></a>