在个人资料页面上显示作者评论

时间:2015-08-27 作者:730wavy

我试图在他们的个人资料页面(author.php)上显示作者的评论,但是我尝试的两个代码似乎都显示了每个人的评论。另外,第二个代码应该链接到特定的注释,但它什么也不做。此外,注释id已经添加到注释输出中,并且可以很好地打印出来。非常感谢您的帮助。

// Method 1
<ul class="authpcom">
<?php
$author_email = get_the_author_meta( \'user_email\' ); 

$args = array(
    \'author_email\' => $author_email
);
$comments = get_comments($args);
foreach($comments as $comment) :
    echo(\'<a href=" \' . get_permalink($comment->post_ID) . \' " rel="external nofollow" title=" \' . $title . \' ">\' .$title . \'</a><br />\' . $comment->comment_date . \'<br /><li>\' . $comment->comment_content . \'</li>\');
endforeach;
?>
</ul>


// Method 2
<?php   $comments = get_comments(); ?>
<ul id="recent_comments">
<?php foreach ($comments as $comment) { ?>
<li><p><strong><?php
        $title = get_the_title($comment->comment_post_ID);
        echo get_avatar( $comment, \'45\' );
echo strip_tags($comment->comment_author); ?></strong>&nbsp;commented on <a href="<?php echo get_permalink($comment->comment_post_ID); ?>#comment-<?php echo $comment->comment_ID; ?>" rel="external nofollow" title="<?php echo $title; ?>"> <?php echo $title; ?></a>: <?php echo wp_html_excerpt( $comment->comment_content, 45 ); ?> (...)</p></li>
<?php }  ?>
</ul>
用于注释输出div-

$comment->comment_ID
object(WP\\u User)#345(7){[“data”]=>object(stdClass)#344(10){[“ID”]=>string(1)”2“[“User\\u login”]=>string(6)”agent1“[“User\\u pass”]=>string(34)”$P$BXUSPFSFBFMYIRJZ2YUNBIS1GWJKDH50“[“User\\u nicename”]=>string(6)”agent1“[“User\\u email”]=>string(19)”[email protected]“[”user\\u url“]=>string(0)”“[”user\\u registered“]=>string(19)”2015-07-25 10:33:27“[”user\\u activation\\u key“]=>string(0)”“[”user\\u status“]=>string(1)”0“[”display\\u name“]=>string(9)”John Paul“}[”ID“]=>int(2)[”caps“=>array(1){[”agent“]=>bool(true)}[”cap\\u key“]=>string(15)”tr\\u功能“[”“roles”]=>数组(1){[0]=>字符串(5)“agent”}[“allcaps”]=>数组(2){[“read”]=>bool(true)[“agent”]=>bool(true)}[“filter”]=>NULL}

3 个回复
最合适的回答,由SO网友:Christine Cooper 整理而成

您需要使用的是WP_Comment_Query() 作用

所以在author.php 页面中,您可以轻松获取作者信息和ID,如下所示:

// get author info
$curauth = (isset($_GET[\'author_name\'])) ? get_user_by(\'slug\', $author_name) : get_userdata(intval($author));

// set ID
$user_id = $curauth->ID;
然后在查询参数数组中添加用户ID:

$args = array(
    \'user_id\' => $user_id, // comments by this user only
    \'status\' => \'approve\',
    \'post_status\' => \'publish\',
    \'post_type\' => \'post\'
);
最后我们在wp_comment_query():

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

// Comment Loop
if ( $comments ) {
    foreach ( $comments as $comment ) {
        echo \'<p>\' . $comment->comment_content . \'</p>\';
    }
} else {
    echo \'No comments found.\';
}
作为额外的奖励,我研究了分页如何与wp_comment_query() 不久前offer a good solution here. 要使它工作起来有点麻烦。

编辑:

获取作者ID的更好方法是使用(props@Pieter):

$user_id = get_queried_object_id();

SO网友:Abhik

第一个方法,缺少的第二个参数get_the_author_meta, 这是作者的身份证。

第二种方法是使用未定义的变量。。检查此代码,应该可以得到您想要的。

// Method 1
<ul class="authpcom">
    <?php
        $queried_object = get_queried_object();
        $author_email = get_the_author_meta( \'user_email\', $queried_object->ID ); 

        $args = array(
            \'author_email\' => $author_email
        );
        $comments = get_comments($args);
        foreach($comments as $comment) :
            echo \'<a href=" \' . get_permalink( $comment->comment_post_ID ) . \' " rel="external nofollow" title=" \' . get_the_title( $comment->comment_post_ID ) . \' ">\' . get_the_title( $comment->comment_post_ID ) . \'</a><br />\' . $comment->comment_date . \'<br /><li>\' . $comment->comment_content . \'</li>\';
        endforeach;
    ?>
</ul>


// Method 2
<?php
    $comments = get_comments();
?>
<ul id="recent_comments">
<?php foreach ($comments as $comment) { ?>
<li>
    <p>
        <strong>
        <?php
            echo get_avatar( $comment->comment_author_email, \'45\' );
            echo strip_tags($comment->comment_author);
        ?>
        </strong>
        &nbsp;commented on <a href="<?php echo get_permalink( $comment->comment_post_ID ); ?>#comment-<?php echo $comment->comment_ID; ?>" rel="external nofollow" title="<?php echo get_the_title( $comment->comment_post_ID ); ?>"> <?php echo get_the_title( $comment->comment_post_ID ); ?></a>: <?php echo wp_html_excerpt( $comment->comment_content, 45 ); ?> (...)
    </p>
</li>
<?php }  ?>
</ul>
EDIT: (2015年5月9日)

// Method 1
<ul class="authpcom">
    <?php
        $authorID = get_queried_object_id();
        $author_email = get_the_author_meta( \'user_email\', $authorID ); 

        $args = array(
            \'user_id\' => $authorID,
        );
        $comments = get_comments($args);
        if ( $comments ) {
            foreach($comments as $comment) {
                echo \'<li><a href="\' . get_permalink( $comment->comment_post_ID ) . \'" rel="external nofollow" title="\' . get_the_title( $comment->comment_post_ID ) . \'">\' . get_the_title( $comment->comment_post_ID ) . \'</a><br>\' . $comment->comment_date . \'<br>\' . $comment->comment_content . \'</li>\';
            }
        } else {
            echo \'<li>No Comments from this Author</li>\';
        }
    ?>
</ul>

SO网友:tillinberlin

您可以使用ultimate member 插件。它似乎可以对用户配置文件做很多事情,但似乎也适合在配置文件页面上显示用户的评论。

&显示作者帖子;对用户配置文件的评论