如何在函数中定位特定的 wp_nav_menu? [英] How to target specific wp_nav_menu in function?

查看:24
本文介绍了如何在函数中定位特定的 wp_nav_menu?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 function.php 在 wp_nav_menu 上添加了一个特定的类,但我无法定位到一个特定的菜单:这就是我得到的

I am adding a specific class on my wp_nav_menu via function.php but I am not able to target a specific menu: This is what I got

function add_menuclass_active($ulclass) {
   return preg_replace('/<a /', '<a class="list-group"', $ulclass, 1);
}
 add_filter('wp_nav_menu', 'add_menuclass_active');

我也尝试过使用,但没有运气

I tried also using with no luck

function add_menuclass_active($ulclass) {
  if( $ulclass['theme_location'] == 'CUSTOM MENU' )
   return preg_replace('/<a /', '<a class="list-group"', $ulclass, 1);
}
add_filter('wp_nav_menu', 'add_menuclass_active');

推荐答案

感谢 来自 wordpress 堆栈的答案 我得到了这个解决方案:

Thanks to an answer from wordpress stack I got this solution:

将此添加到您的functions.php

add this to your functions.php

register_nav_menus(array(
'top-menu' => __('Menu1', 'twentyfourteen'),
'side-menu' => __('Menu2', 'twentyfourteen'),
'footer-menu' => __('Menu3', 'twentyfourteen')
)
);

function my_walker_nav_menu_start_el($item_output, $item, $depth, $args) {
    $menu_locations = get_nav_menu_locations();

    if ( has_term($menu_locations['top-menu'], 'nav_menu', $item) ) {
       $item_output = preg_replace('/<a /', '<a class="list-group" ', $item_output, 1);
    }

    return $item_output;
}
add_filter('walker_nav_menu_start_el', 'my_walker_nav_menu_start_el', 10, 4);

最后,您必须为特定菜单选择选项Menu1",您必须在该菜单上从仪表板 Apperance-> 菜单添加锚定自定义类.[其他锚链接不需要custom-class的菜单选择menu2或menu3]

at last you must select the option "Menu1" for the specific menu on which you have to add the anchor custom classes from dashboard Apperance->menus. [select menu2 or menu3 for other menus whose anchor links does not need the custom-class]

要将活动类"添加到特定菜单的第一个菜单项,请尝试以下操作:

To add "active class" to the first menu item of the particular menu then try this one:

function my_walker_nav_menu_start_el($item_output, $item, $depth, $args) {
    $menu_locations = get_nav_menu_locations();


    if ( has_term($menu_locations['top-menu'], 'nav_menu', $item) ) {
       $item_output = preg_replace('/<a /', '<a class="list-group" ', $item_output, 1);
if ($item->menu_order == 1){
 $item_output = preg_replace('/<a /', '<a class="list-group active" ', $item_output, 1);
}
    }

    return $item_output;
}
add_filter('walker_nav_menu_start_el', 'my_walker_nav_menu_start_el', 10, 4);

如果必须将活动类添加到所有菜单的第一个菜单项,请使用:

if the active class must be added to the first menu item of all menus then use this:

function my_walker_nav_menu_start_el($item_output, $item, $depth, $args) {
    $menu_locations = get_nav_menu_locations();


    if ( has_term($menu_locations['top-menu'], 'nav_menu', $item) ) {
       $item_output = preg_replace('/<a /', '<a class="list-group" ', $item_output, 1);
    }
 if ($item->menu_order == 1){
 $item_output = preg_replace('/<a /', '<a class="active" ', $item_output, 1);
}
    return $item_output;
}
add_filter('walker_nav_menu_start_el', 'my_walker_nav_menu_start_el', 10, 4);

这篇关于如何在函数中定位特定的 wp_nav_menu?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆