So, I understand how to target a specific menu using the following to add a custom code.
<?php
wp_nav_menu(
array(
'menu' => 'Project Nav',
// do not fall back to first non-empty menu
'theme_location' => '__no_such_location',
// do not fall back to wp_page_menu()
'fallback_cb' => false
)
);
?>
Question I have is the following.
Let say I have a menu called “primary”. Within this menu, there are 4 menu items (picture below).
One of the items is called “Profile”.
I want to replace the word “Profile” with the following code which shows user profile photo instead of the word “Profile”.
<?php global $my_profile; ?>
<?php if (is_user_logged_in()) : ?>
<div class="img" data-key="profile"><?php echo get_avatar( get_current_user_id(), 64 ); ?></div>
<?php endif; ?>
So, the final result would look like this:
Now, how can I specifically target a menu item title?
EDIT:
So, I got the following based and I am not sure why it is not working.
function nav_replace_wpse_189788($item_output, $item, $args) {
//var_dump($item_output, $item);
if( $args->theme_location == 'primary' ){
return $items;
if ('royal_profile_menu_title' == $item->title) {
global $my_profile;
if (is_user_logged_in()) {
return '<div class="img" data-key="profile">'.get_avatar( get_current_user_id(), 64 ).'</div>';
}
}
}
return $item_output;
}
add_filter('walker_nav_menu_start_el','nav_replace_wpse_189788',10,2);
Here is my logic. First target the “primary” menu, then look for item title “royal_profile_menu_title” then replace the word title with the output
(My menu title is “royal_profile_menu_title”: Picture below)
Any
2 Answers
This is relatively easily done using the walker_nav_menu_start_el
filter (without all that PHP tag spam):
function nav_replace_wpse_189788($item_output, $item) {
// var_dump($item_output, $item);
if ('Profile' == $item->title) {
global $my_profile; // no idea what this does?
if (is_user_logged_in()) {
return '<div class="img" data-key="profile">'.get_avatar( get_current_user_id(), 64 ).'</div>';
}
}
return $item_output;
}
add_filter('walker_nav_menu_start_el','nav_replace_wpse_189788',10,2);
Note: I had to edit your code to return
a string, not echo
one. You may need to tweak the if
conditional. Uncomment the var_dump()
to see what you have to work with.