浏览页面

时间:2016-07-18 19:59:14

标签: php wordpress uri highlight

我看到了一些关于这个问题的教程,但没有人满意我。我想突出显示我的列表中与我浏览的页面相匹配的单个元素。我用php创建了代码,是一个基于wordpress的网站,代码实际上是有效的,因为当我回应我所在的uri时,它会显示正确的uri,但我创建的if语句是为了在我和#39;网站上的m不会输出任何内容..而且我不明白为什么..反正..这里是代码:

的header.php

<ul class="nav nav-pills sliding" id="Jcollapse">
              <li class="<?php if ($current == "/why-chickapea/"){ echo "current";}?>"><a href="/why-chickapea/"><span>Why Chickapea?</span></a></li>
              <li class="<?php if ($current == "/our-pasta/"){ echo "current";}?>"><a href="/our-story/"><span>Our story</span></a></li>
              <li class="<?php if ($current == "/shop-chickapea/"){ echo "current";}?>"><a href="/shop-chickapea/"><span>Shop</span></a></li>
              <li class="<?php if ($current == "/recipes/"){ echo "current";}?>"><a href="/recipes/"><span>Recipes</span></a></li>
              <li class="<?php if ($current == "/blog/"){ echo "current";}?>"><a href="/blog/"><span>Blog</span></a></li>
            </ul>

在每个页面中我添加了一个php代码段:

<?php $current = $_SERVER["REQUEST_URI"]; ?>

如果我回显var $ current,我将以这种格式获取正确的URL:/ pagename /

最后,我用黄色调了班级电流

.current {
color:yellow;
}
.current a {
color:yellow;
}

有谁知道我的错误在哪里?

这是页面网站:choosechickapea.com

正如您所看到的,我的代码将生成的类是空的,但如果我回显每个值,我将获得的uri是正确的

2 个答案:

答案 0 :(得分:1)

最简单的解释是,在设置$ current之前打印标题。

第二个最简单的解释是不同的范围,意味着要么在非全局范围内设置$ current,要么在非全局范围内读取它,而这两个范围(无论它们是什么)都是不同的。由于有人说wordpress,我想有一些封装到函数中(从而改变了范围)。使用global关键字可能是 a 解决方案,但可能是脏问题。但是因为你已经在避免使用wordpress功能......

答案 1 :(得分:0)

实际代码是:

在标头if语句中声明之前,设置变量的值。如果你在身体中声明,甚至在加载标题之前,例如需要一次或在wordpress中:

 <?php get_header(); ?>

它不起作用,必须在标题中设置变量,如下所示:

<?php $current = $_SERVER["REQUEST_URI"]; ?>
    <header class="navbar-fixed-top">

            <ul class="nav nav-pills sliding">
              <li class="<?php if ($current == "/your-url/"){ echo "current";}?>"><a href="/your-url/"><span>your url</span></a></li>
              <li class="<?php if ($current == "/other-url/"){ echo "current";}?>"><a href="/other-url/"><span>/other url/</span></a></li>
              </ul>
</header>
相关问题