duanchen9594 2013-09-19 17:03
浏览 50
已采纳

将类添加到WordPress中的菜单项

I am creating a theme in WordPress. I want to add a class to a <li> element if the user is on that page.

I have managed to create a dynamic navigation, using following code in my header.php:

<div class="nav">
    <?php wp_nav_menu( array( 'theme_location' => 'nav-menu' ) ); ?>
</div>

Which in HTML, translates to:

<div class="nav">
   <ul>
      <li> Home </li>
      <li> Products </li>
      <li> About </li>
   </ul>
</div>

I am hoping to dynamically alter this. If the user is on www.website.com/about, the navigation will change to:

   <div class="nav">
       <ul>
          <li> Home </li>
          <li> Products </li>
          <li class="underline"> About </li>
       </ul>
    </div>
  • 写回答

1条回答 默认 最新

  • douling0053 2013-09-19 17:09
    关注

    According to the docs, the current page item should already have a class .current-menu-item. you can use that to style the item with underline. Do you not see this class?

    http://codex.wordpress.org/Function_Reference/wp_nav_menu#Current-Page_Menu_Items

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型