Home Backend Development PHP Tutorial WordPress中用于获取及自定义头像图片的PHP脚本详解_php技巧

WordPress中用于获取及自定义头像图片的PHP脚本详解_php技巧

May 16, 2016 pm 08:02 PM
php wordpress avatar

get_avatar()(获取头像)
get_avatar() 函数用来获取置顶邮箱或者用户的头像代码,在评论列表中非常常用。

这个函数提供一个 get_avatar 过滤器,用来过滤头像的 Html 代码(img 标签)。

如果在后台 “设置” 的 “讨论” 里关闭 “显示头像选项” 则返回 False.

用法

get_avatar( $id_or_email, $size, $default, $alt );
Copy after login

参数

$id_or_email

(整数 | 字符串 | 对象)(必须)用户的 ID;邮箱;评论或者用户的对象。如果在循环中可以使用 get_the_author_meta( 'ID' ) 函数调用当前文章的作者。

默认值:None

$size

(整数)(可选)头像的尺寸,最大为 512,单位为像素(px)。

默认值:96
get_avatar()(获取头像)
$default

(字符串)(可选)如果没有头像返回的图片,默认为 “神秘人”,可以是图片 URL.

默认值:空字符串(神秘人)

$alt

(字符串)(可选)头像 img 标签的 alt 属性内容。

默认值:False

返回值

(字符串 | 布尔)返回一个头像的 img 标签,如果在后台 “设置” 的 “讨论” 里关闭 “显示头像选项” 则返回 False.

例子

评论列表:

<&#63;php echo get_avatar( $GLOBALS['comment'] ); &#63;>
Copy after login

循环,获取当前文章作者头像:

<&#63;php echo get_avatar( get_the_author_meta( 'ID' ), 32 ); &#63;>
Copy after login

自定义邮箱:

<&#63;php echo get_avatar( 'email@example.com', 32, '', '自定义邮箱' ); &#63;>

Copy after login


自定义头像图片的 alt 标签
WordPress 默认使用的是 Gravatar 头像,通过 get_avatar() 函数调用,一般用来在调用评论的时候使用。

利用 get_avatar() 函数的第四个属性 $alt 可以设置返回代码的 alt 标签,但是大多数人在使用的时候都不会去设置,也就变成了空白。

搜索引擎的爬虫是无法读取图片上到底是什么内容的,只能靠它的 alt 标签,没有了 alt 标签的图片,对 SEO 是非常不好的。

如果想要给所有的头像都统一加上一个 alt 标签,可以把下边的代码放到主题的 functions.php(了解更多) 文件。

/**
  *WordPress 自定义头像图片的 alt 标签
  *根据不同的人生成不同的 alt 标签
  *http://www.endskin.com/avatar-alt/
*/
function Bing_avatar_alt( $avatar ){
  $alt = 'Gravatar 头像';//alt 标签的内容
  $before = strpos( $avatar, "alt='" );
  $after = strpos( $avatar, "'", $before );
  if( $before === false || $after === false ) return $avatar;
  $alt = esc_attr( $alt );
  return substr( $avatar, 0, $before ) . "alt='$alt" . substr( $avatar, $after + strlen( "'" ) );
}
add_filter( 'get_avatar', 'Bing_avatar_alt' );
Copy after login

这样就行了,注意修改代码里的 $alt 变量为你要修改成的 alt 标签内容。

根据不同的人生成不同的 alt 标签

如果要根据不同的人放置不同的 alt 标签可以用下边的例子代码:

/**
  *WordPress 自定义头像图片的 alt 标签
  *http://www.endskin.com/avatar-alt/
*/
function Bing_avatar_alt( $avatar, $id_or_email ){
  if( $id_or_email == 'bingoblog@163.com' || $id_or_email === 1 ){
    $alt = '博主 Gravatar 头像';//博主的 alt 标签内容
  }else{
    $alt = 'Gravatar 头像';//普通访客的 alt 标签内容
  }
  $before = strpos( $avatar, "alt='" );
  $after = strpos( $avatar, "'", $before );
  if( $before === false || $after === false ) return $avatar;
  $alt = esc_attr( $alt );
  return substr( $avatar, 0, $before ) . "alt='$alt" . substr( $avatar, $after + strlen( "'" ) );
}
add_filter( 'get_avatar', 'Bing_avatar_alt', 10, 2 );
Copy after login

如果已经设置 alt 标签则不替换

如果你需要在已经设置 alt 标签的内容(get_avatar() 函数的 $alt 属性)则不替换的时候可以用下边的代码:

/**
  *WordPress 自定义头像图片的 alt 标签
  *如果已经设置 alt 标签则不替换
  *http://www.endskin.com/avatar-alt/
*/
function Bing_avatar_alt( $avatar, $id_or_email, $size, $default, $alt ){
  if( !empty( $alt ) ) return $avatar;
  $alt = 'Gravatar 头像';//alt 标签的内容
  $before = strpos( $avatar, "alt='" );
  $after = strpos( $avatar, "'", $before );
  if( $before === false || $after === false ) return $avatar;
  $alt = esc_attr( $alt );
  return substr( $avatar, 0, $before ) . "alt='$alt" . substr( $avatar, $after + strlen( "'" ) );
}
add_filter( 'get_avatar', 'Bing_avatar_alt', 10, 5 );
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles