$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>menus - How to target specific wp_nav_menu in function?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

menus - How to target specific wp_nav_menu in function?

matteradmin10PV0评论

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

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 the following with no luck

function add_menuclass_active( $nav_menu, $args ) {
if( $args->theme_location == 'CUSTOM MENU' )
    return preg_replace( '/<a /', '<a class="list-group"', $nav_menu, 1 );
    return $nav_menu;
}
add_filter( 'wp_nav_menu', 'add_menuclass_active', 10, 2 );

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

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 the following with no luck

function add_menuclass_active( $nav_menu, $args ) {
if( $args->theme_location == 'CUSTOM MENU' )
    return preg_replace( '/<a /', '<a class="list-group"', $nav_menu, 1 );
    return $nav_menu;
}
add_filter( 'wp_nav_menu', 'add_menuclass_active', 10, 2 );
Share Improve this question edited Oct 6, 2014 at 8:26 rob.m asked Oct 6, 2014 at 7:38 rob.mrob.m 2072 silver badges9 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 6

I tried the below code and it worked.

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);

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);
Post a comment

comment list (0)

  1. No comments so far