简体   繁体   中英

How to target specific wordpress menus (wp_nav_menu) in a function?

I wanted to create a function which automatically replaces menu items in one specific menu with icons. That worked so far. But I only want the function to target one specific menu and not also the footer menu.

I found a similar post ( How to target specific wp_nav_menu in function? ). But I must say that I'm still too new to PHP and wordpress theme development, so I couldn't transfer that other posts solution to my code.

// Register Navigation
add_action('after_setup_theme', 'cwb_register_nav');

function cwb_register_nav() {
    register_nav_menus(array(
        'header_nav_main' => 'Header Main Navigation Menu',
        'header_nav_social' => 'Header Social Menu',
        // 'posts_categories_menu' => 'Posts Categories Menu',
        'footer_sitemap_menu' => 'Footer Sitemap Menu',
        'footer_legal_menu' => 'Footer Legal Menu',
        'footer_social_menu' => 'Footer Social Menu'
    ));
}

// Replacing Text with Icons in the Navigation, for example Facebook & Twitter

add_filter('wp_nav_menu', 'my_page_menu_link_names');

function my_page_menu_link_names($menu) {
    $menu = str_replace('Facebook', '<i class="icon facebook"></i>', $menu);
    $menu = str_replace('Twitter', '<i class="icon twitter"></i>', $menu);
    $menu = str_replace('Instagram', '<i class="icon instagram"></i>', $menu);
    $menu = str_replace('LinkedIn', '<i class="icon linkedin"></i>', $menu);
    $menu = str_replace('Google Plus', '<i class="icon googleplus"></i>', $menu);
    $menu = str_replace('Pinterest', '<i class="icon pinterest"></i>', $menu);
    $menu = str_replace('RSS', '<i class="icon rss"></i>', $menu);
    $menu = str_replace('Whatsapp', '<i class="icon whatsapp"></i>', $menu);
    return $menu;
}

Again, I want the 'my_page_menu_link_names' function to only affect the 'header_nav_social' menu and don't affect the 'footer_social_menu' for example, like how it does so far.

I found a solution myself. I don't know why my previous attempts with 'theme_location' didn't worked.

// Replacing Text with Icons in the Navigation, for example Facebook & Twitter
    add_filter( 'wp_nav_menu_items', 'my_page_menu_link_names', 10, 2 );
    function my_page_menu_link_names( $menu, $args ) {
        if ($args->theme_location == 'header_nav_social') {
            $menu = str_replace( 'Facebook', '<i class="icon facebook"></i>', $menu );
            $menu = str_replace('Twitter', '<i class="icon twitter"></i>', $menu );
            $menu = str_replace('Instagram', '<i class="icon instagram"></i>', $menu );
            $menu = str_replace('LinkedIn', '<i class="icon linkedin"></i>', $menu );
            $menu = str_replace('Google Plus', '<i class="icon googleplus"></i>', $menu );
            $menu = str_replace('Pinterest', '<i class="icon pinterest"></i>', $menu );
            $menu = str_replace('RSS', '<i class="icon rss"></i>', $menu );
            $menu = str_replace('Whatsapp', '<i class="icon whatsapp"></i>', $menu );
            return $menu;
        }
        return $menu;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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