PHP 标头包含导航菜单问题

发布于 2024-12-10 01:41:01 字数 894 浏览 0 评论 0原文

我正在使用 PHP include 来输入我的标头,其中当然包含我的导航菜单。

导航菜单部分的“header.php”代码如下:

$nav = array("Home","About","Portfolio","Products","Services","Contact");

foreach($nav as $item)
{
     $class = '';
     $href = $item;

     if($item == "Home")
     {
         $class = 'class="current-menu-ancestor parent"';
         $href = 'index';
     }elseif($item == $title){ 
         $class = 'class="current-menu-ancestor parent"';
     }

     echo "<li $class><a href='$href.php'>$item</a></li>";
}

抱歉,如果它有点混乱。 它从相关页面获取 $title ,将显示在页面上的示例代码是:

    $title = "Home";
    include("header.php");

现在一切似乎都工作正常,正确的导航菜单项在那里,它们都链接到正确的位置等,“home”实际上是“index.php”,而不是上面“header.php”中编码的“home.php”。

在“主页”页面上时,“主页”页面导航菜单项会突出显示,以表明用户位于该页面上,但是,当我转到任何其他页面时,例如“关于”页面,它突出显示“关于”导航菜单项和“主页”导航菜单项。

我哪里错了?

I am using a PHP include to input my header, which of course, contains my navigation menu.

The 'header.php' code for the navigation menu part is below:

$nav = array("Home","About","Portfolio","Products","Services","Contact");

foreach($nav as $item)
{
     $class = '';
     $href = $item;

     if($item == "Home")
     {
         $class = 'class="current-menu-ancestor parent"';
         $href = 'index';
     }elseif($item == $title){ 
         $class = 'class="current-menu-ancestor parent"';
     }

     echo "<li $class><a href='$href.php'>$item</a></li>";
}

Sorry if its a bit messy.
It gets $title from the relevant page, example code that would be displayed on a page is:

    $title = "Home";
    include("header.php");

Now it all seems to work fine, the right navigational menu items are there, they all link to right places etc, 'home' is in fact 'index.php' not 'home.php' as coded in 'header.php' above.

When on the 'home' page, the 'home' page nav menu item is highlighted as it should be to indicate that the user is on that page, however, when I go to any other page, say for example the 'about' page, it highlights the 'about' nav menu item and also the 'home' nav menu item.

Where am I going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

謸气贵蔟 2024-12-17 01:41:01

您的 foreach 循环将始终将“Home”的 $class 放置为“current-menu-ancestor”,假设这就是使其突出显示的原因。您可能需要执行如下操作:

foreach($nav as $item)
{
    $class = '';
    $href = $item;
    if($item == "Home") 
    {
        // $class = 'class="current-menu-ancestor parent"'; Get rid of this
        $href = 'index';
    }
    if($item == $title) // Change this to if instead of elseif
    {
        $class = 'class="current-menu-ancestor parent"';
    }
    echo "<li $class><a href='$href.php'>$item</a></li>";
}

Your foreach loop will always place the $class for "Home" as "current-menu-ancestor", assuming that's what makes it highlighted.. You might want to do something like below:

foreach($nav as $item)
{
    $class = '';
    $href = $item;
    if($item == "Home") 
    {
        // $class = 'class="current-menu-ancestor parent"'; Get rid of this
        $href = 'index';
    }
    if($item == $title) // Change this to if instead of elseif
    {
        $class = 'class="current-menu-ancestor parent"';
    }
    echo "<li $class><a href='$href.php'>$item</a></li>";
}
谈情不如逗狗 2024-12-17 01:41:01

尝试使用

if($title == "Home")

而不是

if($item == "Home")

你是如此接近!

Try using

if($title == "Home")

rather than

if($item == "Home")

You were so close!

踏月而来 2024-12-17 01:41:01
$nav = array("Home","About","Portfolio","Products","Services","Contact");
foreach($nav as $item){
    $class = '';
    $href = $item;
    if ($item == "Home") {
        $href = 'index';
    }
    if ($item == $title) {
        $class = 'class="current-menu-ancestor parent"';
    }
    echo "<li $class><a href='$href.php'>$item</a></li>";
}

是正确的代码。这只是你的条件逻辑中的一点混乱。

$nav = array("Home","About","Portfolio","Products","Services","Contact");
foreach($nav as $item){
    $class = '';
    $href = $item;
    if ($item == "Home") {
        $href = 'index';
    }
    if ($item == $title) {
        $class = 'class="current-menu-ancestor parent"';
    }
    echo "<li $class><a href='$href.php'>$item</a></li>";
}

Is the correct code. It was just a little mixup in your conditional logic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文