[英]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? ). 我找到了类似的帖子( 如何在功能中定位特定的wp_nav_menu? )。 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.
但是我必须说,我对PHP和wordpress主题开发还不太陌生,因此无法将其他帖子解决方案转移到我的代码中。
// 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. 同样,我希望“ my_page_menu_link_names”功能仅影响“ header_nav_social”菜单,而不会影响“ footer_social_menu”,例如到目前为止。
I found a solution myself. 我自己找到了解决方案。 I don't know why my previous attempts with 'theme_location' didn't worked.
我不知道为什么以前使用“ theme_location”的尝试没有奏效。
// 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.