How to target specific wp_nav_menu in function?

前端 未结 2 1349
夕颜
夕颜 2021-01-16 06:38

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         


        
相关标签:
2条回答
  • 2021-01-16 07:18

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

    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);
    
    0 讨论(0)
  • 2021-01-16 07:39

    Theme location is stored in the nav menu arguments, you need to add it in your function parameters:

    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 );
    
    0 讨论(0)
提交回复
热议问题