如何在 Wordpress Post 循环中获取 URL 到 Post 作者的用户 BuddyPress 头像

How To Get URL To Post Author's User BuddyPress Avatar Inside Wordpress Post Loop

问题

如何将 URL 设为 post 作者的头像?

要求

我想要 url 到我网站上托管的图像。

图像将在 post.

的循环中调用

作者post就是我要显示头像的人

我试过的

一堆东西,但主要是<?php echo get_avatar_url('user_email'); ?>

它 returns url 到由 Gravatar 托管的默认 'mystery man' 图片,尽管它们是为该测试用户上传到我的网站的头像。

澄清

我不想要 Gravatar 托管图片。

参考

Wordpress code reference for get_avatar_url()

目前没有完整的 Wordpress Codex 条目

担忧

我开始认为头像和 Gravatar 之间的所有 WP 歧义 get_avatar_url 仅适用于 Gravatar 图片,当然只对 Gravatar 注册用户可用。希望有人能告诉我我错了。

可能感兴趣

似乎没有关于此的文档,难怪它一直具有挑战性。

为了检索 BuddyPress 头像 URL,请使用如下函数:

// Assuming $user_id is the id for the desired user
echo bp_core_fetch_avatar ( 
    array(  'item_id' => $user_id, // id of user for desired avatar
            'type'    => 'full',
            'html'   => FALSE     // FALSE = return url, TRUE (default) = return img html
    ) 
);

这是来自 php 源文件的文档(由我稍微格式化):

文档

bp_core_fetch_avatar( $args );

Return: 字符串格式化 HTML <img> 元素,或基于 $[=231= 的原始头像 URL ] arg.

描述:

获取 BuddyPress 的头像 object。 默认支持用户、群组和博客的头像,但可以 也扩展为支持自定义组件。

此功能优先于locally-uploaded 头像。当一个当地人 找不到头像,查询Gravatar

用法

<?php  bp_core_fetch_avatar( $args ); ?>

默认用法:

<?php  $args = array(
    'item_id'       => false,
    'object'        => 'user',
    'type'          => 'thumb',
    'avatar_dir'    => false,
    'width'         => false,
    'height'        => false,
    'class'         => 'avatar',
    'css_id'        => false,
    'title'         => false,
    'alt'           => '',
    'email'         => false,
    'no_grav'       => false,
    'html'          => true,
    'extra_attr'    => '',
    'scheme'        => null,
    'rating'        => {setting for 'avatar rating' option},
    'force_default' => false
);

bp_core_fetch_avatar( $args ); ?>

参数:

所有参数在技术上都是可选的;如果没有提供,有些将由 bp_core_fetch_avatar() auto-detected。这个auto-detection在下面讨论具体论点时有更多描述。

item_id
(int|bool) 您为其请求头像的项目的数字 ID(例如,用户 ID)。如果不存在 'item_id',该函数会尝试从 'object' + 当前上下文推断 ID:如果 'object' 是 'user' 并且当前页面是用户页面, 'item_id' 将默认为显示的用户ID;如果 'group' 并且在群组页面上,则为当前群组 ID;如果 'blog',到当前博客的 ID。如果这样无法确定'item_id',则函数return为假。 默认值:false。

$object
(字符串)您要为其获取头像的 object 类型。 BuddyPress 原生支持三个选项:'user'、'group'、'blog';一个插件可能会注册更多。 默认:'user'.

$type
(string) 当一个新头像上传到 BP 时,'thumb' 和 'full' 版本被保存。此参数指定您想要 'full' 还是更小的 'thumb' 头像。 默认值:'thumb'.

$avatar_dir
(string|bool) (auto-detected) 应在其中找到请求的头像的子目录的名称。如果没有传值,'avatar_dir'是从'object'推导出来的:'user'变成'avatars','group'变成'group-avatars','blog'变成'blog-avatars'.

记住这个字符串表示BP的主头像目录的一个子目录(通常基于{@link wp_upload_dir()});它是一个类似于 'group-avatars' 的字符串,而不是完整的目录路径。通常,如果将头像存储在 non-default 位置,则只需要覆盖默认值。 默认值:false。

$width
(int|bool) (auto-detected) 请求的头像宽度。单位是像素。该值用于为 <img> 元素构建 'width' 属性。如果没有传递任何值,BP 将使用全局头像宽度作为该头像类型。 默认值:false。

$身高
(int|bool) (auto-detected) 请求的头像高度。单位是像素。该值用于为 <img> 元素构建 'height' 属性。如果没有传递任何值,BP 将使用全局头像高度作为该头像类型。 默认值:false。

$class (字符串)<img> 元素的 CSS class。请注意,BP 在其默认样式中相当广泛地使用 'avatar' class,因此如果您打算传递自定义值,请考虑将其附加到 'avatar'(例如 'avatar foo')而不是而不是完全替换它。 默认值:'avatar'.

$css_id (string|bool) <img> 元素的 CSS id。 默认值:false。

$title (字符串)<img> 元素的标题属性。 默认值:false。

$alt (字符串)<img> 元素的 alt 属性。在 BP 中,这个值通常由包装函数传递,其中连接字符串所需的数据就在手边;看 {@link bp_get_activity_avatar()} 举个例子。 默认值:''。

$email
(string|bool) 在 Gravatar 查询中使用的电子邮件。除非另有配置,否则 BP 使用 Gravatar 作为本地未提供的头像的后备。 Gravatar 的 API 需要使用用户电子邮件地址的哈希值;这个论点提供了它。如果未提供,该函数将推断它:对于用户,通过从数据库中获取用户的电子邮件,对于 groups/blogs,通过连接“{$item_id}-{$object}@ {bp_get_root_domain()}”。用户查询会增加开销,因此建议包装函数在查询用户 ID 时为 'email' 提供一个值。 默认值:false。

$no_grav (bool) 是否禁用默认的 Gravatar 回退。默认情况下,BP 在找不到本地头像时会回退到 Gravatar。在某些情况下,这可能是不可取的,在这种情况下 'no_grav' 应该设置为 true。要全局禁用 Gravatar 回退,请参阅'bp_core_fetch_avatar_no_grav' 过滤器。 默认值:false。

$html
(bool) 是否 return 一个 <img> HTML 元素,与一个原始的 URL 到一个化身。如果为 false,<img>-specific arguments (like 'css_id') 将被忽略。 默认值:真。

$extra_attr (字符串)HTML 要插入 IMG 元素的属性。没有消毒。 默认值:''。

$方案
(字符串)URL 要使用的方案。有关可接受的值,请参阅 set_url_scheme()。 默认为空。

$评级 (字符串)显示 Gravatars 的评级。接受 'G'、'PG'、'R'、'X'。 默认值为 'avatar_rating' 选项的值。

$force_default (bool) 在创建 Gravatar URL 时使用。是否强制默认 图像,无论 Gravatar 是否存在。 默认值:false。

备注:

要在本地禁用 Gravatar 查询回退,请添加此过滤器:

add_filter( 'bp_core_fetch_avatar_no_grav', '__return_true' );

我不确定这个答案是否包含在 Cale 的答案中,所以我取消删除它以防它对其他人有用。

在循环中(比如 single.php)

<img class="" src="<?php 
$authorUserID = get_the_author_meta('ID');  // get the user id of the post author
echo bp_core_fetch_avatar ( 
    array(  'item_id'   => $authorUserID,       // output user id of post author
            'type'      => 'full',
            'html'      => FALSE               // FALSE = return url, TRUE (default) = return url wrapped with html and css classes
    ) 
); 
?>">

或从主题文件夹中的 functions.php 文件调用

调用(从你的循环中说 single.php)

<?php custom_get_author_avatar_url(); ?>

函数(放在functions.php中)

function custom_get_author_avatar_url() {
    $authorUserID = get_the_author_meta('ID');  // get the user id of the post author
    echo bp_core_fetch_avatar ( 
    array(  'item_id'   => $authorUserID,       // output user id of post author
            'type'      => 'full',
            'html'      => FALSE            // FALSE = return url, TRUE (default) = return url wrapped with html and css classes
    ) 
    ); 
}