【问题标题】:How to display a user's avatar by user-id in wordpress via shortcode?如何通过简码在wordpress中通过用户ID显示用户的头像?
【发布时间】:2016-07-30 19:59:18
【问题描述】:

我查看了很多文档,其中很少有显示使用 user_id 显示头像的简码。 最接近的来自 Github,它显示当前登录的用户,如下所示:

<?php
function shortcode_user_avatar() {
    if(is_user_logged_in()) { // check if user is logged in
        global $current_user; // get current user's information
        get_currentuserinfo();
        return get_avatar( $current_user -> ID, 24 ); // display the logged-in user's avatar
    }
    else {
      // if not logged in, show default avatar. change URL to show your own default avatar
        return get_avatar( 'http://1.gravatar.com/avatar/ad524503a11cd5ca435acc9bb6523536?s=64', 24 );
    }
}
add_shortcode('display-user-avatar','shortcode_user_avatar');
?>

但这还不够,我想要添加一个参数让我选择用户ID,它会像这样结束: [display-user-avatar id="user-id"]

谁能告诉我制作方法? 谢谢!

【问题讨论】:

    标签: wordpress shortcode avatar


    【解决方案1】:

    这个问题我已经解决了,代码如下:

    function shortcode_user_avatar($atts, $content = null) {
       extract( shortcode_atts( 
               array('id' => '0',), $atts 
                              ) 
               );
    
       return get_avatar( $user_id, 96 ); // display the specific user_id's avatar  
                                                           }
    add_shortcode('avatar','shortcode_user_avatar');
    

    只需将其粘贴到主题的functions.php并输入短代码[avatar id="xxx"],并将“xxx”替换为用户ID。

    这实际上是我的第一个简码,我真的很高兴它能正常工作!

    【讨论】:

    • 不,这是不正确的,在返回输入:return get_avatar($atts['id'], 96);
    猜你喜欢
    • 2012-07-11
    • 2016-08-11
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多