duanlanzhi5509 2016-07-18 19:59
浏览 14
已采纳

在浏览页面时突出显示列表元素

I saw a few tutorial about this problem but none of them satisfied me. I want to highlight the single element of my list which matches the page that I'm browsing. I created the code with php, is a wordpress based website, and the code actually works because when I echo the uri which I'm on it will display the right uri, but the if statement I created to add a class when I'm on the website won't output anything.. and I don't understand why.. anyway.. here's the code:

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>

In each page I added a php snippet:

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

If I echo the var $current I will obtain the right url in this format: /pagename/

At the end I style the class current with a yellow color

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

Does anyone know where my mistake is?

this is the page website: choosechickapea.com

As you can see the class that my code will generate is empty, but if I echo each value the uri I will obtain is the right one

  • 写回答

2条回答 默认 最新

  • douhe3313 2016-07-19 15:16
    关注

    The simplest explanation would be, that you print the header before $current is set.

    The second simplest explanation is different scopes, meaning either you set $current in a non-global scope or you read it in a non-global scope, and those two (whatever they are) are different. Since someone said wordpress, I guess there is some encapsulation into functions (thus changing the scope). Using the global keyword may be a solution, but a dirty one. But since you're already avoiding wordpress functions ...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了