Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get post author name in WordPress?

In WordPress I need to fetch name of author who created post using author_id.
How can I find author_name ?

like image 456
Rakhi Prajapati Avatar asked Mar 18 '17 08:03

Rakhi Prajapati


People also ask

How do I find the post author name in WordPress?

the_author( string $deprecated = '', bool $deprecated_echo = true. Displays the name of the author of the current post.

How do I find the author email in WordPress?

You can get by get_the_author_meta() wordpress function to get author email address. <? php $user_email = get_the_author_meta('user_email'); ?> Get the email address for user ID 25, and echo it using their display name as the anchor text.

How do I change the author of a WordPress post?

To change the author of an existing post, go to your admin area > Posts. Click on Quick Edit > Author and from the drop-down menu choose the new author and click on Update.


5 Answers

You can use get_the_author_meta(), to get author data.

echo get_the_author_meta('display_name', $author_id);

Hope this helps!

like image 196
Raunak Gupta Avatar answered Oct 21 '22 07:10

Raunak Gupta


This should work like charm

<?php echo get_the_author(); ?>

For more detailed information. https://codex.wordpress.org/Function_Reference/get_the_author

like image 20
Amit Avatar answered Oct 21 '22 09:10

Amit


Use below code in single.php or the relevant page you want author name

<?php get_the_author_meta( 'display_name', $author_id ); ?>
like image 28
Nikhil Chopde Avatar answered Oct 21 '22 08:10

Nikhil Chopde


Add This code in single-post.php

<?php echo get_the_author(); ?>

I hope this will work !!

like image 24
Jawwad Rizwan Avatar answered Oct 21 '22 08:10

Jawwad Rizwan


When used in the WordPress Custom REST API endpoint you can do it like this:

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;
}
like image 33
MuturiAlex Avatar answered Oct 21 '22 09:10

MuturiAlex