如何在WordPress中获取作者姓名?

Rak*_*ati 5 php mysql wordpress posts hook-wordpress

在WordPress中,我需要获取使用创建帖子的作者的姓名author_id.
我怎么找到author_name

Rau*_*pta 18

您可以使用get_the_author_meta(),来获取作者数据.

echo get_the_author_meta('display_name', $author_id);
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!

  • `get_the_author_meta()` 没有折旧。您可能会想到“get_the_author()” (2认同)

Ami*_*mit 6

这应该像魅力一样工作

<?php echo get_the_author(); ?>
Run Code Online (Sandbox Code Playgroud)

如需更多详细信息。 https://codex.wordpress.org/Function_Reference/get_the_author


小智 6

在 single.php 或您想要作者姓名的相关页面中使用以下代码

<?php get_the_author_meta( 'display_name', $author_id ); ?>
Run Code Online (Sandbox Code Playgroud)


小智 5

在 WordPress 自定义 REST API 端点中使用时,您可以这样做:

function customrestapiplugin_getpost( $slug ) {
    $args = [
        'name' => $slug['slug'],
        'post_type' => 'post'
    ];

    $post = get_posts($args);
    
    $data[$i]['id'] = $post[0]->ID;
        $data['title'] = $post[0]->post_title;
        $data['content'] = $post[0]->post_content;
        $data['excerpt'] = $post[0]->post_excerpt;
        $data['slug'] = $post[0]->post_name;
        $data['date'] = $post[0]->post_date;
        $data['link'] = get_permalink($post[0]->ID);
        $data['author'] = get_the_author_meta('display_name', $post[0]->post_author);
        $data['featured_image']['thumbnail'] = get_the_post_thumbnail_url($post[0]->ID, 'thumbnail');
        $data['featured_image']['medium'] = get_the_post_thumbnail_url($post[0]->ID, 'medium');
        $data['featured_image']['large'] = get_the_post_thumbnail_url($post[0]->ID, 'large');

    return $data;
}
Run Code Online (Sandbox Code Playgroud)