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 badges
Add a comment  | 

2 Answers 2

Reset to default 1

Works 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