简体   繁体   English

将活动类添加到 Wordpress 中当前选定的锚点

[英]Add active class to current selected anchor in Wordpress

How to add active class to my anchor in Wordpress?如何在 Wordpress 中将活动类添加到我的锚点? Current code is below:当前代码如下:

$args = array(
    'theme_location' => 'primary',
    'container' => 'div',
    'container_class' => 'collapse navbar-collapse',
    'menu_class' => 'nav navbar-nav navbar-right'
);
wp_nav_menu( $args );

I get this output for each menu item:我为每个菜单项得到这个输出:

<li id="menu-item-42" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-42">
    <a href="http://localhost:8888/axial/services/">Services</a>
</li>

But I want the 'a' tag to have an 'active' class, like I have shown below:但我希望 'a' 标签有一个 'active' 类,如下所示:

<li id="menu-item-42" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-42">
    <a class="active" href="http://localhost:8888/axial/services/">Services</a>
</li>

Yes it's possible, try code below:是的,有可能,请尝试以下代码:

function add_class_to_href( $classes, $item ) {
    if ( in_array('current_page_item', $item->classes) ) {
        $classes['class'] = 'active';
    }
    return $classes;
}
add_filter( 'nav_menu_link_attributes', 'add_class_to_href', 10, 2 );

Just paste this code into functions.php file:只需将此代码粘贴到functions.php文件中:

 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 '; } return $classes; }

You can do this with CSS.你可以用 CSS 做到这一点。

you can use current-menu-item class which is added by default.您可以使用默认添加的 current-menu-item 类。

 /*you have to add the active menu item styles like this*/ a{ padding:5px; font-family:arial; color:#333; text-decoration:none; } .navbar-right ul li{ display:inline-block; list-style-type:none; border:1px solid #bbb; padding:5px; } .navbar-right li.current-menu-item > a { color:#fff; background:#bbb; /*you can add the active anchor styles here*/ }
 <nav class="navbar-right"> <ul> <li class="current-menu-item"><a href="/home/">Home</a></li> <li class="menu-item"><a href="/about/">About</a></li> <li class="menu-item"><a href="/contact/">Contact</a></li> </ul> </nav>

Today most of the time we have to deal with dropdown menus in wordpress so,just to improvise the above answer, you can use:今天大部分时间我们必须处理 wordpress 中的下拉菜单,所以,为了即兴回答上述问题,您可以使用:

add_filter('nav_menu_css_class', 'add_active_class', 10, 2 );

function add_active_class($classes, $item) {
  if( $item->menu_item_parent == 0 && in_array('current-menu-item', $classes) ) {
      $classes[] = "active";
   }
   return $classes;
 }

you can add active class for child elements using the below code: 
add_filter('nav_menu_css_class', 'add_active_class', 10, 2 );
function add_active_class($classes, $item)
{
  if( in_array( 'current-menu-item', $classes ) ||
  in_array( 'current-menu-ancestor', $classes ) ||
  in_array( 'current-menu-parent', $classes ) ||
  in_array( 'current_page_parent', $classes ) ||
  in_array( 'current_page_ancestor', $classes )) 
    {
       $classes[] = "active";
    }
   return $classes;
}

I also face the same problem and after searching many page I could only find the solution that I have to set it using jquery code.我也面临同样的问题,在搜索了很多页面后,我只能找到我必须使用 jquery 代码设置它的解决方案。 so I find a unique class addition in my page ".current_page_item" you might have the same or ".current_menu_item" etc.. so just include the following jquery code to your footer and your page will work fine.所以我在我的页面“.current_page_item”中找到了一个独特的类添加,你可能有相同的或“.current_menu_item”等。所以只需在你的页脚中包含以下jquery代码,你的页面就会正常工作。

$(".current_page_item a").addClass("active");

Do hop this will solve your problem.跳这将解决您的问题。

You can add "nav-link" class to all anchor tags while having the "active" class on current menu item only by using this code.您可以将“导航链接”类添加到所有锚标记,同时仅使用此代码在当前菜单项上具有“活动”类。

/**
 * Add CSS class for "<a>" tags in menu
 */
function your_theme_add_menu_link_class( $classes, $item ) {
    $classes['class'] = "nav-link"; // Add class to every "<a>" tags
    if ( in_array('current_page_item', $item->classes) ) {
        $classes['class'] = 'nav-link active'; // Add class to current menu item's "<a>" tag
    }
    return $classes;
}
add_filter( 'nav_menu_link_attributes', 'your_theme_add_menu_link_class', 10, 2 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM