簡體   English   中英

在 Wordpress 導航菜單中顯示登錄的用戶頭像

[英]Display logged-in User Avatar in Wordpress Nav Menu

我正在使用一個函數在 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中有這段代碼,我在菜單項中用#current-username#調用它,有沒有辦法自定義這段代碼來輸出頭像? 謝謝你的幫助。

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

您可以像這樣顯示用戶名。

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;
}

嘗試以下插件: https : //wordpress.org/plugins/ava-the-shortcode/,並使用其他插件向登錄的用戶顯示或不顯示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM