如何获取URL以在Wordpress Post循环内发布作者的用户BuddyPress头像 [英] How To Get URL To Post Author's User BuddyPress Avatar Inside Wordpress Post Loop

查看:93
本文介绍了如何获取URL以在Wordpress Post循环内发布作者的用户BuddyPress头像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

如何仅获取帖子作者头像的URL?

How to get just the URL to a post author's avatar?

要求

我想要网站上托管的图像的URL.

I want the url to the image that is hosted on my website.

图片将在帖子循环中被调用.

Image will be called in the loop of a post.

帖子的作者是我希望显示其头像的人.

The author of the post is the person whose avatar image I wish to display.

我尝试过的事情

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

尽管该头像是该测试用户上传到我网站上的化身,但它仍返回由Gravatar托管的默认神秘男人"图片的网址.

It returns a url to the default 'mystery man' image hosted by Gravatar despite their being an avatar uploaded to my site for this test user.

说明

我不希望Gravatar托管图片.

I don't want a Gravatar hosted image.

要引用

get_avatar_url()的Wordpress代码参考

当前没有完整的Wordpress Codex条目

There is currently no completed Wordpress Codex entry for it

关注点

我开始考虑到头像和Gravatar之间的所有WP模糊性,get_avatar_url仅用于Gravatar图像,这些图像当然仅适用于Gravatar注册用户.希望有人能告诉我我错了.

I'm beginning to think with all the WP ambiguity between avatar and Gravatar that get_avatar_url is solely for Gravatar images which are of course only available to Gravatar registered users. Hopefully someone can tell me I am wrong.

可能的兴趣

  • 网站使用Buddypress

  • Website uses Buddypress

此网站的Buddypress安装使用自定义头像大小

This websites Buddypress installation uses custom avatar sizes

该网站使用其自己的自定义主题

This website uses its own custom theme

用户已将头像上传到网站

User has avatar uploaded to website

用户没有Gravatar帐户(不进行测试,因为大多数用户不会拥有Gravatar帐户.我不希望大多数用户使用神秘男人代替他们的化身)

User does not have a gravatar account (testing without as most users wont have gravatar accounts. I dont want most users to have mystery men in place of their avatars)

以下代码可以很好地提取网站托管的头像(但当然输出图像标记和类,而不是我需要的纯URL)

The following code works fine to pull the website hosted avatar (but of course outputs the image tag and classes, not the pure url that i require)

<?php echo get_avatar( get_the_author_meta( 'ID' ), 180 ); ?>

推荐答案

似乎没有关于此的文档,所以难怪它具有挑战性.

There doesn't seem to be an documentation around this, so no wonder it has been challenging.

为了检索BuddyPress头像URL,请使用如下功能:

In order to retrieve the BuddyPress avatar URL, use the function like so:

// 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文件中的文档(由我自己格式化):

Here is the documentation (formatted a bit by me) from the source php file:

bp_core_fetch_avatar( $args );

返回:字符串格式化的HTML <img>元素,或基于$ html arg的原始头像URL.

Return: string Formatted HTML <img> element, or raw avatar URL based on $html arg.

获取BuddyPress对象的化身. 默认情况下,支持用户,组和博客的化身,但可以是 扩展为也支持自定义组件.

Get an avatar for a BuddyPress object. Supports avatars for users, groups, and blogs by default, but can be extended to support custom components as well.

此功能优先处理本地上传的头像.当当地人 找不到化身,查询了化身.

This function gives precedence to locally-uploaded avatars. When a local avatar is not found, Gravatar is queried.

<?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()自动检测.在讨论特定参数时,将在下面对自动检测进行详细说明.

Parameters:

All arguments are technically optional; some will, if not provided, be auto-detected by bp_core_fetch_avatar(). This auto-detection is described more below, when discussing specific arguments.

item_id
(int | bool)您要为其请求头像的商品的数字ID(例如,用户ID).如果不存在"item_id",则该函数尝试从对象" +当前上下文中推断出一个ID:如果对象"为用户"且当前页面为用户页面,则"item_id"将默认显示为用户身份;如果是"group",并且在组页面上,则为当前组ID;如果是'blog',则为当前博客的ID.如果无法以此方式确定'item_id',则该函数返回false. 默认:false.

item_id
(int|bool) The numeric ID of the item for which you're requesting an avatar (eg, a user ID). If no 'item_id' is present, the function attempts to infer an ID from the 'object' + the current context: if 'object' is 'user' and the current page is a user page, 'item_id' will default to the displayed user ID; if 'group' and on a group page, to the current group ID; if 'blog', to the current blog's ID. If no 'item_id' can be determined in this way, the function returns false. Default: false.

$ object
(字符串)您要为其化身的对象的类型. BuddyPress本机支持三个选项:用户",组",博客";插件可能会注册更多. 默认:用户".

$object
(string) The kind of object for which you're getting an avatar. BuddyPress natively supports three options: 'user', 'group', 'blog'; a plugin may register more. Default: 'user'.

$ type
(字符串)将新的头像上传到BP时,将保存缩略图"和完整"版本.此参数指定您是否要使用完整"或较小的拇指"化身. 默认:拇指".

$type
(string) When a new avatar is uploaded to BP, 'thumb' and 'full' versions are saved. This parameter specifies whether you'd like the 'full' or smaller 'thumb' avatar. Default: 'thumb'.

$ avatar_dir
(字符串|布尔)(自动检测)应在其中找到所请求的头像的子目录的名称.如果未传递任何值,则从对象"推断出"avatar_dir":用户"变为头像",组"变为组头像",博客"变为博客头像".

$avatar_dir
(string|bool) (auto-detected) The name of the subdirectory where the requested avatar should be found. If no value is passed, 'avatar_dir' is inferred from 'object': 'user' becomes 'avatars', 'group' becomes 'group-avatars', 'blog' becomes 'blog-avatars'.

请记住,此字符串表示BP主头像目录的子目录(通常基于{@link wp_upload_dir()});它是一个类似于"group-avatars"的字符串,而不是完整的目录路径.通常,仅当将化身存储在非默认位置时才需要覆盖默认值. 默认:false.

Remember that this string denotes a subdirectory of BP's main avatar directory (usually based on {@link wp_upload_dir()}); it's a string like 'group-avatars' rather than the full directory path. Generally, it'll only be necessary to override the default value if storing avatars in a non-default location. Default: false.

$ width
(int | bool)(自动检测)要求的化身宽度.单位是px.此值用于构建<img>元素的'width'属性.如果未传递任何值,则BP使用此化身类型的全局化身宽度. 默认:false.

$width
(int|bool) (auto-detected) Requested avatar width. The unit is px. This value is used to build the 'width' attribute for the <img> element. If no value is passed, BP uses the global avatar width for this avatar type. Default: false.

$高度
(int | bool)(自动检测到)请求的头像高度.单位是px.此值用于为<img>元素构建'height'属性.如果未传递任何值,则BP使用此化身类型的全局化身高度. 默认:false.

$height
(int|bool) (auto-detected) Requested avatar height. The unit is px. This value is used to build the 'height' attribute for the <img> element. If no value is passed, BP uses the global avatar height for this avatar type. Default: false.

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

$class (string) The CSS class for the <img> element. Note that BP uses the 'avatar' class fairly extensively in its default styling, so if you plan to pass a custom value, consider appending it to 'avatar' (eg 'avatar foo') rather than replacing it altogether. Default: 'avatar'.

$ css_id (字符串|布尔)<img>元素的CSS ID. 默认:false.

$css_id (string|bool) The CSS id for the <img> element. Default: false.

$ title (字符串)<img>元素的title属性. 默认:false.

$title (string) The title attribute for the <img> element. Default: false.

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

$alt (string) The alt attribute for the <img> element. In BP, this value is generally passed by the wrapper functions, where the data necessary for concatenating the string is at hand; see {@link bp_get_activity_avatar()} for an example. Default: ''.

$ email
(string | bool)在Gravatar查询中使用的电子邮件.除非另行配置,否则BP会将Gravatar用作未在本地提供的化身的后备. Gravatar的API需要使用用户电子邮件地址的哈希值;这个论点提供了它.如果未提供,该函数将推断出该信息:对于用户而言,是从数据库中获取用户的电子邮件,对于群组/博客而言,是通过串联"{$ item_id}-{$ object} @ {bp_get_root_domain()}".用户查询会增加开销,因此建议包装函数在查询用户ID时为电子邮件"提供一个值. 默认:false.

$email
(string|bool) An email to use in Gravatar queries. Unless otherwise configured, BP uses Gravatar as a fallback for avatars that are not provided locally. Gravatar's API requires using a hash of the user's email address; this argument provides it. If not provided, the function will infer it: for users, by getting the user's email from the database, for groups/blogs, by concatenating "{$item_id}-{$object}@{bp_get_root_domain()}". The user query adds overhead, so it's recommended that wrapper functions provide a value for 'email' when querying user IDs. Default: false.

$ no_grav (布尔)是否禁用默认的Gravatar后备.默认情况下,当BP无法找到本地化身时,它将回退到Gravatar.在某些情况下,这可能是不可取的,在这种情况下,应将"no_grav"设置为true.要全局禁用Gravatar后备,请参见"bp_core_fetch_avatar_no_grav"过滤器. 默认:false.

$no_grav (bool) Whether to disable the default Gravatar fallback. By default, BP will fall back on Gravatar when it cannot find a local avatar. In some cases, this may be undesirable, in which case 'no_grav' should be set to true. To disable Gravatar fallbacks globally, see the 'bp_core_fetch_avatar_no_grav' filter. Default: false.

$ html
(布尔)是否返回<img> HTML元素,而不是返回化身的原始URL.如果为false,则将忽略<img>特定的参数(例如'css_id'). 默认:是.

$html
(bool) Whether to return an <img> HTML element, vs a raw URL to an avatar. If false, <img>-specific arguments (like 'css_id') will be ignored. Default: true.

$ extra_attr (字符串)要插入IMG元素的HTML属性.未消毒. 默认:".

$extra_attr (string) HTML attributes to insert in the IMG element. Not sanitized. Default: ''.

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

$scheme
(string) URL scheme to use. See set_url_scheme() for accepted values. Default null.

$ rating (字符串)显示墓碑"的评级.接受"G","PG","R","X". 默认值为"avatar_rating"选项的值.

$rating (string) What rating to display Gravatars for. Accepts 'G', 'PG', 'R', 'X'. Default is the value of the 'avatar_rating' option.

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

$force_default (bool) Used when creating the Gravatar URL. Whether to force the default image regardless if the Gravatar exists. Default: false.

要在本地禁用Gravatar查询后备,请添加以下过滤器:

To disable Gravatar query fallbacks locally, add this filter:

add_filter( 'bp_core_fetch_avatar_no_grav', '__return_true' );

这篇关于如何获取URL以在Wordpress Post循环内发布作者的用户BuddyPress头像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆