Reputation: 11
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.
Upvotes: 0
Views: 488
Reputation: 11
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;
}
Upvotes: 1