当前浏览:首页 / WordPress函数 / get_the_author_meta()

get_the_author_meta( string $field = '', int|false $user_id = false ): string

检索当前文章作者的请求数据

author 作者more...

metamore...

themore...


描述

$field参数的有效值包括:

  • admin_color
  • aim
  • comment_shortcuts
  • description
  • display_name
  • first_name
  • ID
  • jabber
  • last_name
  • nickname
  • plugins_last_view
  • plugins_per_page
  • rich_editing
  • syntax_highlighting
  • user_activation_key
  • user_description
  • user_email
  • user_firstname
  • user_lastname
  • user_level
  • user_login
  • user_nicename
  • user_pass
  • user_registered
  • user_status
  • user_url
  • yim

参数

$fieldstring可选
要检索的用户字段。

默认:''

$user_idint|false可选
用户ID。

默认:false


返回

string 当前作者的DB对象中的作者字段,否则为空字符串。


更多信息

如果在循环中使用,则无需指定用户ID,默认为当前文章作者。如果在循环外部使用,则必须指定用户ID。

get_the_author_meta()返回在PHP中以编程方式使用的数据。要显示它,请使用the_author_meta()

如果此用户不存在指定的meta字段,则返回空字符串。

插件可以向用户配置文件中添加额外的字段,这又会向wp_usermeta数据库表中添加新的key/value对。可以通过将字段的键作为$field参数传递给函数来检索此附加数据。


钩子



源码

查看源码 官方文档


更新日志

版本描述
2.8.0开始引入

使用示例

  • 示例1

    显示作者简介(description),保持换行

    <?php echo nl2br(get_the_author_meta('description')); ?>
  • 示例2

    在循环外获取作者ID:

    global $post;
    $author_id = $post->post_author;

    在循环中获取作者ID:

     $author_id = get_the_author_meta( 'ID' ); 

    下面是作者值的几个例子:

    // to get  nicename
    get_the_author_meta( 'nicename', $author_id );
    
    // to get  email
    get_the_author_meta( 'email', $author_id );
    
    // to get  url
    get_the_author_meta( 'url', $author_id );
    
    // to get  status
    get_the_author_meta( 'status', $author_id );
  • 示例3

    对description使用wpautop()也会保留换行符(如studio-jt的评论),但会输出更干净的html:

    /**
     * Display Author's description 
     * with keeping line breaks and wrapping its paragraphs
     * into tag
     *
     * @link https://developer.wordpress.org/reference/functions/wpautop/
     */
    echo wpautop( get_the_author_meta( 'description' ) );
  • 示例4

    获取作者ID:

    <?php $user_id = get_the_author_meta( 'ID' ); ?>
  • 示例5

    使用电子邮件地址链接显示用户的显示名称
    获取用户ID 25的电子邮件地址,并将其显示名称作为锚文本进行回显。

    <p>Email the author: 
        <a href="mailto:<?php echo get_the_author_meta( 'user_email', 25 ); ?>">
            <?php the_author_meta( 'display_name', 25 ); ?>
        </a>
    </p>
  • 示例6

    获取用户的电子邮件地址
    获取当前文章作者的电子邮件地址,并将其存储在$user_email变量中以供进一步使用。(请记住,此函数返回数据,但不显示数据。)

    <?php $user_email = get_the_author_meta( 'user_email' ); ?>
  • 示例7

    我可以在“field”中使用的作者meta是什么?

    可以将它们添加到此页面吗?或者可以添加到另一个列出它们的页面的链接?