admin管理员组

文章数量:1296312

I'm trying to display a list of the author comments in the authors page but it only shows No comments made.

How can I list user comments in the authors page?

<?php
$args = array(
    'user_id' => $user->ID,
    'number' => 10, // how many comments to retrieve
    'status' => 'approve'
    );

$comments = get_comments( $args );

if ( $comments )
{
    $output.= "<ul>\n";
    foreach ( $comments as $c )
    {
    $output.= '<li>';
    $output.= '<a href="'.get_comment_link( $c->comment_ID ).'">';
    $output.= get_the_title($c->comment_post_ID);
    $output.= '</a>, Posted on: '. mysql2date('m/d/Y', $c->comment_date, $translate);
    $output.= "</li>\n";
    }
    $output.= '</ul>';

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

I'm trying to display a list of the author comments in the authors page but it only shows No comments made.

How can I list user comments in the authors page?

<?php
$args = array(
    'user_id' => $user->ID,
    'number' => 10, // how many comments to retrieve
    'status' => 'approve'
    );

$comments = get_comments( $args );

if ( $comments )
{
    $output.= "<ul>\n";
    foreach ( $comments as $c )
    {
    $output.= '<li>';
    $output.= '<a href="'.get_comment_link( $c->comment_ID ).'">';
    $output.= get_the_title($c->comment_post_ID);
    $output.= '</a>, Posted on: '. mysql2date('m/d/Y', $c->comment_date, $translate);
    $output.= "</li>\n";
    }
    $output.= '</ul>';

    echo $output;
} else { echo "No comments made";} ?>
Share Improve this question edited Dec 20, 2015 at 21:58 Sven 3,6841 gold badge35 silver badges48 bronze badges asked Dec 20, 2015 at 18:52 Frank OlsonFrank Olson 114 bronze badges 1
  • Is $user->ID set ? What happens if you remove the 'user_id' part? I assume you got plenty of approved comments. – birgire Commented Dec 20, 2015 at 19:12
Add a comment  | 

1 Answer 1

Reset to default 2

Try with WP_Comment_Query and make sure you have the right Author ID from the Author Template.

// WP_Comment_Query arguments
$args = array (
    'user_id'        => $user->ID,
    'post_status'    => 'approve',
    'number'         => '10',
);

// The Comment Query
$comments = new WP_Comment_Query;
$comments = $comments->query( $args );

// The Comment Loop
if ( $comments ) {

    $output.= "<ul>\n";

    foreach ( $comments as $c ) {
        $output.= '<li>';
        $output.= '<a href="'.get_comment_link( $c->comment_ID ).'">';
        $output.= get_the_title($c->comment_post_ID);
        $output.= '</a>, Posted on: '. mysql2date('m/d/Y', $c->comment_date, $translate);
        $output.= "</li>\n";
    }

    $output.= '</ul>';

    echo $output;
} else {
    echo 'No comments found.';
}

本文标签: List user comments in author page