Javascript 在wordpress中添加自定义ul的活动链接

Javascript 在wordpress中添加自定义ul的活动链接,javascript,php,jquery,wordpress,Javascript,Php,Jquery,Wordpress,我在wordpress边栏中有一个自定义列表(ul)。我想将活动类添加到列表项中 <ul class="sidebar-list"> <li> <a href="index.php/owle/">OWLE</a></li> <li><a href="index.php/sink-or-swim/">Sink or Swim</a></li> <li><a href

我在wordpress边栏中有一个自定义列表(ul)。我想将活动类添加到列表项中

<ul  class="sidebar-list">
 <li> <a href="index.php/owle/">OWLE</a></li>
 <li><a href="index.php/sink-or-swim/">Sink or Swim</a></li>
 <li><a href="index.php/swim-and-survive/">Swim and Survive</a></li>
 <li><a href="index.php/vwsc/">VWSC</a></li>
 <li><a href="index.php/water-smart/">Water Smart</a></li>
 <li><a href="index.php/grey-medallion/">Grey Medallion</a></li>
 <li><a href="index.php/aquatic-pd-workshops/">Aquatic PD Workshops</a></li>

 <li><a href="index.php/edu-from-anywhere/">Edu From Anywhere</a></li>
 <li><a href="index.php/bronze-e-lifesaving/">Bronze e-Lifesaving</a></li>
 <li><a href="index.php/water-smart-award/">Water Smart Award</a></li>
  <li><a href="index.php/victorian-water-safety-certificate/">Victorian Water Safety Certificate</a></li>
 <li><a href="index.php/edu-from-anywhere-newsletter/">Edu From Anywhere Newsletter</a></li>
 <li><a href="index.php/edu-casual-instructor/">Edu Casual Instructor</a></li>

 <li><a href="index.php/grey-medallion/">Grey Medallion</a></li>
 <li><a href="index.php/sink-or-swim/">Sink or Swim</a></li>
 <li><a href="index.php/edu-instructor-of-year-profile/">Edu Instructor of year Profile</a></li>
  <li><a href="index.php/swim-survive-licensee-of-year/">Swim & Survive Licensee of year</a></li>
   </ul>


<li class="active"> <a href="index.php/owle/">OWLE</a></li>
  • 应该与“index.php/owle/”上的类似

    add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
    function special_nav_class($classes, $item){
     if( in_array('current-menu-item', $classes) ){
             $classes[] = 'active ';  // your new class
     }
     return $classes;
    
    }

    试试这段代码,它可以工作。您所要做的就是用您正在使用的类更改这些类名


    你不需要为它编码

    使用wordpress菜单创建侧栏

    wordpress菜单具有当前项的默认类

    当前菜单项


    您可以向其中添加css

    将此添加到wordpress的自定义javascript中。很多主题都有这个。检查主题定制

     var url = window.location;
     // Will only work if string in href matches with location
     $('sidebar-list li a[href="'+ url +'"]').parent().addClass('active');
    
     // Will also work for relative and absolute hrefs
      $('sidebar-list li a').filter(function() {
        return this.href == url;
     }).parent().addClass('active');  
    

    如果你的主题没有这个,你必须硬编码这个。我建议您将其添加到页脚中。php

    活动类添加到所有列表项中?不将活动类添加到当前页面的列表项中。当用户在“index.php/owle/”上时,类似于
  • 。就像wordpress主导航上的当前菜单项一样。