get_avatar() 是 WordPress 默认获取的 Gravatar 头像的函数,函数通过用户 ID 或邮件地址获取用户头像,同时如果要正确显示头像需要将设置/讨论面板下的“显示头像”给勾选上。该函数的定义位于 wp-includes 文件夹下的 pluggable.php 中,相关代码如下:
if ( !function_exists( 'get_avatar' ) ) : /** * Retrieve the avatar `<img>` tag for a user, email address, MD5 hash, comment, or post. * * @since 2.5.0 * @since 4.2.0 Optional `$args` parameter added. * * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash, * user email, WP_User object, WP_Post object, or WP_Comment object. * @param int $size Optional. Height and width of the avatar image file in pixels. Default 96. * @param string $default Optional. URL for the default image or a default type. Accepts '404' * (return a 404 instead of a default image), 'retro' (8bit), 'monsterid' * (monster), 'wavatar' (cartoon face), 'indenticon' (the "quilt"), * 'mystery', 'mm', or 'mysteryman' (The Oyster Man), 'blank' (transparent GIF), * or 'gravatar_default' (the Gravatar logo). Default is the value of the * 'avatar_default' option, with a fallback of 'mystery'. * @param string $alt Optional. Alternative text to use in <img> tag. Default empty. * @param array $args { * Optional. Extra arguments to retrieve the avatar. * * @type int $height Display height of the avatar in pixels. Defaults to $size. * @type int $width Display width of the avatar in pixels. Defaults to $size. * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false. * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are * judged in that order. Default is the value of the 'avatar_rating' option. * @type string $scheme URL scheme to use. See set_url_scheme() for accepted values. * Default null. * @type array|string $class Array or string of additional classes to add to the <img> element. * Default null. * @type bool $force_display Whether to always show the avatar - ignores the show_avatars option. * Default false. * @type string $extra_attr HTML attributes to insert in the IMG element. Is not sanitized. Default empty. * } * @return false|string `<img>` tag for the user's avatar. False on failure. */ function get_avatar( $id_or_email, $size = 96, $default = '', $alt = '', $args = null ) { $defaults = array( // get_avatar_data() args. 'size' => 96, 'height' => null, 'width' => null, 'default' => get_option( 'avatar_default', 'mystery' ), 'force_default' => false, 'rating' => get_option( 'avatar_rating' ), 'scheme' => null, 'alt' => '', 'class' => null, 'force_display' => false, 'extra_attr' => '', ); if ( empty( $args ) ) { $args = array(); } $args['size'] = (int) $size; $args['default'] = $default; $args['alt'] = $alt; $args = wp_parse_args( $args, $defaults ); if ( empty( $args['height'] ) ) { $args['height'] = $args['size']; } if ( empty( $args['width'] ) ) { $args['width'] = $args['size']; } if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) { $id_or_email = get_comment( $id_or_email ); } /** * Filters whether to retrieve the avatar URL early. * * Passing a non-null value will effectively short-circuit get_avatar(), passing * the value through the {@see 'get_avatar'} filter and returning early. * * @since 4.2.0 * * @param string $avatar HTML for the user's avatar. Default null. * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash, * user email, WP_User object, WP_Post object, or WP_Comment object. * @param array $args Arguments passed to get_avatar_url(), after processing. */ $avatar = apply_filters( 'pre_get_avatar', null, $id_or_email, $args ); if ( ! is_null( $avatar ) ) { /** This filter is documented in wp-includes/pluggable.php */ return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args ); } if ( ! $args['force_display'] && ! get_option( 'show_avatars' ) ) { return false; } $url2x = get_avatar_url( $id_or_email, array_merge( $args, array( 'size' => $args['size'] * 2 ) ) ); $args = get_avatar_data( $id_or_email, $args ); $url = $args['url']; if ( ! $url || is_wp_error( $url ) ) { return false; } $class = array( 'avatar', 'avatar-' . (int) $args['size'], 'photo' ); if ( ! $args['found_avatar'] || $args['force_default'] ) { $class[] = 'avatar-default'; } if ( $args['class'] ) { if ( is_array( $args['class'] ) ) { $class = array_merge( $class, $args['class'] ); } else { $class[] = $args['class']; } } $avatar = sprintf( "<img alt='%s' src='%s' srcset='%s' class='%s' height='%d' width='%d' %s/>", esc_attr( $args['alt'] ), esc_url( $url ), esc_url( $url2x ) . ' 2x', esc_attr( join( ' ', $class ) ), (int) $args['height'], (int) $args['width'], $args['extra_attr'] ); /** * Filters the avatar to retrieve. * * @since 2.5.0 * @since 4.2.0 The `$args` parameter was added. * * @param string $avatar <img> tag for the user's avatar. * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash, * user email, WP_User object, WP_Post object, or WP_Comment object. * @param int $size Square avatar width and height in pixels to retrieve. * @param string $default URL for the default image or a default type. Accepts '404', 'retro', 'monsterid', * 'wavatar', 'indenticon','mystery' (or 'mm', or 'mysteryman'), 'blank', or 'gravatar_default'. * Default is the value of the 'avatar_default' option, with a fallback of 'mystery'. * @param string $alt Alternative text to use in the avatar image tag. Default empty. * @param array $args Arguments passed to get_avatar_data(), after processing. */ return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args ); } endif;
使用 get_avatar() 函数
对于 get_avatar() 函数的使用我们一般可以通过以下方式调用:
<?php echo get_avatar( $id_or_email, $size, $default, $alt, $args ); ?>
$id_or_email:必填(整型/字符串/对象)。用户 ID 、邮件地址或者评论区评论对象。
$size:选填(整型)。要返回的 Gravatar 头像的大小,最大为 512 默认 96 。
$default:选填(字符串)。自定义的默认头像地址,默认“神秘人物”头像。
$alt:选填(字符串)。img 标签的 alt 属性值,图像的替换文本,默认为空。
$args:选填(数组)。设置新的参数以替换 get_avatar中 的默认参数。
$args 可设置的参数
如果要设置新的参数以替换 get_avatar中 的默认参数,必须在调用 get_avatar 之前声明新的参数,否则它们将不会生效。以下参数均为可选参数,由于使用较少,此处仅简单说下,具体内容请参阅文后官方文档!
size(整型、大小)、height(整型、高度)、width(整型、宽度)、default(字符串、默认头像)、force_default(布尔型、强制使用默认头像)、rating(字符串、头像等级 ‘G’, ‘PG’, ‘R’, ‘X’)、scheme(字符串,设置 URL 的 Scheme)、class(数组或字符串,设置 img 标签的 class 属性)、force_display(布尔型、是否强制显示 Gravatar 头像即使未勾选“显示头像”)、extra_attr(字符串,设置头像的 attributes 属性)。
官方文档:https://codex.wordpress.org/Function_Reference/get_avatar
评论 (3)
如果我想让 当鼠标碰到这个头像转动 该怎么做
我照着这个网页做 结果发现找不到这个头像的地址
https://www.mochoublog.com/study/194.html
求赐教
改下CSS应该就可以了哇,之前博客分享过文章的
666