PHP使用includes在页面上设置活动链接 [英] PHP set active link on page using includes

查看:94
本文介绍了PHP使用includes在页面上设置活动链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我搜索SO一小时试图找到答案,并尝试了各种方法,包括这个

So I have searched SO for an hour trying to find the answer, and also tried various methods, including this

我试图包含我的页面,随着导航。但是在正确的页面上,我需要list-item有一个活动的类。导航位于header.php中,目前看起来像这样:

I am trying to include my pages, along with the navigation. But on the correct page, I need the list-item to have a class of active. The navigation is in header.php and currently looks like this:

<nav>
    <ul>
      <li class="active"> <a href="/">Home</a> </li>
      <li> <a href="#">Apps</a> </li>
      <li> <a href="#">Forums</a> </li>
    </ul>
  </nav>

首先,我不知道JS(jQuery)是否更好,或者PHP是否是更好。如果它有效,两者都无关紧要。

First, I have no idea if JS(jQuery) would be better, or if PHP was to be better. Both wouldn't matter if it works.

另外我对PHP有点新,并试图学习。

Also I am a bit new with PHP and trying to learn.

最简单(希望)的方法是什么?所以我不需要更改很多代码只是为了给导航 class =active

What would be the easiest (hopefully) method to use? So I don't have to change a lot of code just to give a nav class="active"

推荐答案

Asumming你有一个 $ page 变量(包含你当前所在页面的名称):

Asumming you have a $page variable (which contains the name of the page you are currently on):

  <nav>
    <ul>
      <li class="<?php echo ($page == "home" ? "active" : "")?>"> <a href="/">Home</a> </li>
      <li class="<?php echo ($page == "apps" ? "active" : "")?>"> <a href="#">Apps</a> </li>
      <li class="<?php echo ($page == "forums" ? "active" : "")?>"> <a href="#">Forums</a> </li>
    </ul>
  </nav>

这篇关于PHP使用includes在页面上设置活动链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆