我想知道是否有办法在wordpress中获得当前登录用户头像的URI/URL?我发现这是一种使用get_avatar (放在主题functions.php中的php下面)生成插入当前用户头像的短代码的方法:
<?php
function logged_in_user_avatar_shortcode() {
if ( is_user_logged_in() ) {
global $current_user;
get_currentuserinfo();
return get_avatar( $current_user->ID );
}
}
add_shortcode('logged-in-user-avatar', 'logged_in_user_avatar_shortcode');
?>但是,这将返回整个图像,包括属性(img src、class、width、height、alt)。我只想返回URL,因为我已经在模板中为我的图像设置了所有属性。
尝试制作这样的东西:
<img src="[shortcode-for-avatar-url]" class="myclass" etc >有谁知道这样做的方法吗?
非常感谢你提前
发布于 2014-01-23 22:37:59
您可以使用preg_match查找URL:
function logged_in_user_avatar_shortcode()
{
if ( is_user_logged_in() )
{
global $current_user;
$avatar = get_avatar( $current_user->ID );
preg_match("/src=(['\"])(.*?)\1/", $avatar, $match);
return $match[2];
}
}
add_shortcode('logged-in-user-avatar', 'logged_in_user_avatar_shortcode');发布于 2014-01-23 23:40:46
在最近的WordPress安装中,我编写了一个PHP函数来获取用户gravatar,如果WordPress低于2.5版,我的函数使用了一种不同的方式来检索用户gravatar。可以在下面找到一个稍微修改过的版本,它只是简单地输出用户的gravatar URI。
// Fallback for WP < 2.5
global $post;
$gravatar_post_id = get_queried_object_id();
$gravatar_author_id = get_post_field('post_author', $gravatar_post_id) || $post->post_author;//get_the_author_meta('ID');
$gravatar_email = get_the_author_meta('user_email', $gravatar_author_id);
$gravatar_hash = md5(strtolower(trim($gravatar_email)));
$gravatar_size = 68;
$gravatar_default = urlencode('mm');
$gravatar_rating = 'PG';
$gravatar_uri = 'http://www.gravatar.com/avatar/'.$gravatar_hash.'.jpg?s='.$gravatar_size.'&d='.$gravatar_default.'&r='.$gravatar_rating.'';
echo $gravatar_uri; // URI of GRAVATARhttps://stackoverflow.com/questions/21310487
复制相似问题