How to put the author of the post in the comments?

I need to put the name of the author who wrote the post and I can’t find the solution.

EXAMPLE:

Rachel [06/03/21] : Commented in the post “How to be a Web Designer”, written by: POSTAGE AUTHOR – Test Comment

<?php
    $args = array(
        'user_id' => bp_current_user_id(),
        'number' => 20, // how many comments to retrieve
        'status' => 'approve'
        );

    $comments = get_comments( $args );

    if ( $comments )
    {
        $output.= "<ul>n";
        foreach ( $comments as $c )
        { 
        $output.= '<li>';
        $output.= $c->comment_author;
        $output.= ' [';
        $output.= mysql2date('d/m/y', $c->comment_date, $translate);
        $output.= '] ';
        $output.= ' : ';
        $output.= '<a href="'.get_comment_link( $c->comment_ID ).'">';
        $output.= get_the_title($c->comment_post_ID);
        $output.= "</a>";
        $output.= ' - ';
        $output.= $comment->user_id;
        $output.= $c->comment_content;
        $output.= "</li>n";
        
        }
        $output.= '</ul>';

        echo $output;
        
        
    } else { echo "No comments made";}
?>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Not sure it’s the shortest way, but it works)

Get post author id

$post_author_id = get_post_field( 'post_author', $c->comment_post_ID );

Get author data by id

$post_author_display_name = get_the_author_meta( 'display_name', $post_author_id );

Try this shorter version

$output.= get_the_author_meta( 'display_name', get_post_field( 'post_author', $c->comment_post_ID ) );

Check documentation page which data is available with get_the_author_meta()


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x