admin管理员组文章数量:1333611
Using this code:
add_filter( 'wp_nav_menu_items', 'my_custom_menu_item');
function my_custom_menu_item($items)
{
if(is_user_logged_in())
{
$user=wp_get_current_user();
$name=$user->display_name; // or user_login , user_firstname, user_lastname
$items .= '<li><a href="">Welcome '.$name.'</a></li>';
}
return $items;
}
I am able to display the current logged in user's display Name into the navigation, but it replaces the current menu items.
What would I need to make the Display name the parent item which I can then use the wordpress menu system for the sub items ? I assume id need some kind of placeholder in the menu like #place_holder# but am unsure how to get to that stage.
Using this code:
add_filter( 'wp_nav_menu_items', 'my_custom_menu_item');
function my_custom_menu_item($items)
{
if(is_user_logged_in())
{
$user=wp_get_current_user();
$name=$user->display_name; // or user_login , user_firstname, user_lastname
$items .= '<li><a href="">Welcome '.$name.'</a></li>';
}
return $items;
}
I am able to display the current logged in user's display Name into the navigation, but it replaces the current menu items.
What would I need to make the Display name the parent item which I can then use the wordpress menu system for the sub items ? I assume id need some kind of placeholder in the menu like #place_holder# but am unsure how to get to that stage.
Share Improve this question asked Oct 9, 2017 at 20:37 Randomer11Randomer11 41611 silver badges31 bronze badges2 Answers
Reset to default 1Works fine for me, nothing is replaced, it's just appended to the navigation. Are you sure that it's not just a styling-issue? Have you looked at the document source to check if it really is replacing the other menu items?
What exactly do you mean by making it the parent item? Do you want to turn
<li><a href="">Item</a></li>
into
<li><a href="">Welcome $user</a>
<ul>
<li><a href="">Item</a></li>
</ul>
</li>
in the end?
To change your menu and make it easy & maintainable, maybe you could use something like this:
add_filter( 'wp_nav_menu_objects', 'my_custom_menu_item');
function my_custom_menu_item($items) {
$remove_childs_of = array();
foreach($items as $index => $item) {
if($item->title == "##currentuser##") {
if(is_user_logged_in()) {
$user=wp_get_current_user();
$name=$user->display_name; // or user_login , user_firstname, user_lastname
$items[$index]->title = $name;
}
else {
array_push($remove_childs_of, $item->ID);
unset($items[$index]);
}
}
if(!empty($remove_childs_of) && in_array($item->menu_item_parent, $remove_childs_of)) {
array_push($remove_childs_of, $item->ID);
unset($items[$index]);
}
}
return $items;
}
The wp_nav_menu_objects
filter allows us to work on the items before they get transformed into HTML. You'll have to add an Item to your menu that shows "##currentuser##". If a user is logged in, this menu item will show his name afterwards. If it is an anonymous user, the menu item and its submenues will be removed from the navigation.
//here is the simple code to display current or logged in username to display his her name // just code do widget section add php code widget wherever you want then add the following //codes
user_login ) ) . ''; ?>
本文标签: navigationDisplay Username as parent menu item
版权声明:本文标题:navigation - Display Username as parent menu item 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742280336a2445977.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论