如何通过短代码在wordpress中通过user-id显示用户的头像?

时间:2016-04-09 14:59:27

标签: wordpress shortcode avatar

我查看过很多文档,其中很少有人使用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"]

任何人都可以告诉我如何制作它吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,这里是代码:

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');

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

这实际上是我的第一个短代码,我很高兴它正在工作!

相关问题