duanchi19820419 2013-01-07 17:30
浏览 45
已采纳

Wordpress菜单突出显示

I am new to Wordpress. My version is WP 3.4.2 The site is on my localhost, sorry I cannot share it.

How do I dynamically highlight a nav menu item based on current post? This question appears here on Stack Overflow quite a bit, but I have not found any accepted answers. Some answers I have seen here use Javascript but I cannot because 20% Of my visitors don't have js.

I found a solution, using filter hooks ( a new concept to me ). I have added the filter_hook below, in header.php.

This hook fires on the correct posts, but the final result is wrong. Instead of adding a class name "current-menu-item" to 1 item in my menu, My whole menu is just the string:"current-menu-item";

Can anyone please help me understand what I have done wrong?

if(($post->post_type) =="communities")
    add_filter('wp_nav_menu' , 'special_nav_class' , 10 , 2);

function special_nav_class( $item){

     if(true){
             $class = "current-menu-item";
     }
     return $class;
}
  • 写回答

2条回答 默认 最新

  • dongzhina7098 2013-01-07 18:18
    关注

    If you use WordPress embedded menu functionality (and you definitely should) WordPress automatically adds current-menu-item class to the current item, and current-menu-parent to the main LI, if the current item is in a drop-down menu.

    Then all you have to do is apply your CSS rules to these classes.

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题