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 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM