5

In my site I made two different menus. One is for visitors and the other is for logged in members. Here are the codes I've used -

<php?
if( is_user_logged_in() ) {
$menu = 'Login Menu';}
else {
$menu = 'Main Menu'; }
wp_nav_menu( array( 'menu' => $menu, 'container_class' => 'menu-header', 'theme_location' => 'primary' ) ); ?>

Now on " Main Menu " I have a button called " Register ". And a button named " Log In ". I want to make this button appears as " Welcome [Login Member's name] " and " Sign Out " respectively when a member log in. And as far as I know this code will go under the " Login Menu ", So how can I do that ?

4

1 回答 1

8

将以下代码粘贴到您的functions.php

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;
}
于 2012-08-01T06:16:15.177 回答