Hello I am using the below code in my functions.php of my buddypress install to add a 'My profile' link to my header menu. This works great, however it is also automatically adding a link to each of my footer menus.
I only want to add this link to the header menu with the menu id '12'. Can someone help me modify the code?
Thank you
// Filter wp_nav_menu() to add profile link
add_filter( 'wp_nav_menu_items', 'my_nav_menu_profile_link' );
function my_nav_menu_profile_link($menu) {
if (!is_user_logged_in())
return $menu;
else
$profilelink = '<li><a href="' . bp_loggedin_user_domain( '/' ) . '">' . __('My Profile') . '</a></li>';
$menu = $menu . $profilelink;
return $menu;
}
Dbranes answers:
Here is one idea:
// Filter wp_nav_menu() to add profile link
add_filter( 'wp_nav_menu_items', 'my_nav_menu_profile_link' );
function my_nav_menu_profile_link($menu) {
if (!is_user_logged_in())
return $menu;
else
remove_filter( 'wp_nav_menu_items', 'my_nav_menu_profile_link' );
$profilelink = '<li><a href="' . bp_loggedin_user_domain( '/' ) . '">' . __('My Profile') . '</a></li>';
$menu = $menu . $profilelink;
return $menu;
}
Ross Gosling comments:
Thanks Dbranes, not sure what you did here, but it worked.
Just to ask if I also want to add 'My profile' to menu id 27. (so menu's 12 & 27) is that possible?
Dbranes comments:
strange, I can't see my answer ;-)
instead of using
add_filter( 'wp_nav_menu_items', 'my_nav_menu_profile_link' );
you can try
add_filter( 'wp_nav_menu_{$menu->slug}_items', 'my_nav_menu_profile_link' );
where you replace <em>{$menu->slug}</em> with your menu slug.
Ross Gosling comments:
Thank you Dbranes :)
Doug Montgomery answers:
Let me ask...wouldn't it be easier to create a couple of Wordpress custom menus and use one in your theme and widget menu in the footer?
Just askin...Maybe I'm missing something.
Ross Gosling comments:
Hi, I have 5 custom menus created in wordpress: Header, Footer1, Footer2, Footer3, Footer4.
The above code shows 'My profile' link in each of these, I only want it to show on menu 'Header'.
Giri answers:
// Filter wp_nav_menu() to add profile link
add_filter( 'wp_nav_menu_items', 'my_nav_menu_profile_link' );
function my_nav_menu_profile_link($menu) {
if (!is_user_logged_in()) {
return $menu;
} else {
if ($menu = 12) {
$profilelink = '<li><a href="' . bp_loggedin_user_domain( '/' ) . '">' . __('My Profile') . '</a></li>';
$menu = $menu . $profilelink;
return $menu;
} else {
return $menu;
}
}
}
Giri comments:
If thats not work try this too (added quotes in menu id)
// Filter wp_nav_menu() to add profile link
add_filter( 'wp_nav_menu_items', 'my_nav_menu_profile_link' );
function my_nav_menu_profile_link($menu) {
if (!is_user_logged_in()) {
return $menu;
} else {
if ($menu = "12") {
$profilelink = '<li><a href="' . bp_loggedin_user_domain( '/' ) . '">' . __('My Profile') . '</a></li>';
$menu = $menu . $profilelink;
return $menu;
} else {
return $menu;
}
}
}
Giri comments:
Also try like this.. Instead of using menu id... use your menu's theme location (change theme location in my code)
// Filter wp_nav_menu() to add profile link
add_filter( 'wp_nav_menu_items', 'my_nav_menu_profile_link',20, 2 );
function my_nav_menu_profile_link($nav, $args) {
if (!is_user_logged_in()) {
return $menu;
} else {
if ($args->theme_location == 'primary_navigation') {
$profilelink = '<li><a href="' . bp_loggedin_user_domain( '/' ) . '">' . __('My Profile') . '</a></li>';
$menu = $menu . $profilelink;
return $menu;
} else {
return $menu;
}
}
}