2015-06-20 71 views
2

我正在使用一個功能在WordPress導航菜單中顯示已登錄的用戶名,並且我還想在菜單中顯示已登錄的用戶頭像,但找不到一種方法來做到這一點。所以我問一些幫助:) 這裏是我使用的名稱的功能:在Wordpress導航菜單中顯示已登錄的用戶頭像

function my_dynamic_menu_items($menu_items) { 
    foreach ($menu_items as $menu_item) { 
     if ('#current-username#' == $menu_item->title) { 
      global $shortcode_tags; 
      if (isset($shortcode_tags['current-username'])) { 
       // Or do_shortcode(), if you must. 
       $menu_item->title = call_user_func($shortcode_tags['current-username']); 
      }  
     } 
    } 

    return $menu_items; 
} 
add_filter('wp_nav_menu_objects', 'my_dynamic_menu_items'); 

我有我的functions.php這個代碼,我用#當前戶名#中調用它一個菜單項,有沒有辦法自定義這個代碼也輸出頭像?謝謝你的幫助。

回答

0

試試這個:echo get_avatar($id_or_email, $size, $default, $alt);

0

可以顯示用戶名等。

add_filter('wp_nav_menu_items', 'your_custom_menu_item', 10, 2); 
function your_custom_menu_item ($items, $args) { 
    $current_user = wp_get_current_user(); 
    if(!empty($current_user->user_login)) 
     $items .= '<li><a href="javascript:void(0)">'.$current_user->user_login.'</a></li>';   
    return $items; 
} 
+0

感謝Mansukh,對於最近的回覆感到抱歉,這有助於我管理它:) – Neokentin

+0

好的。好,所以我的答案給予聲譽... –