在带有最近评论的列表中-每页/帖子仅显示一条评论(最新评论)

时间:2013-01-12 作者:AWK

我正在尝试制作一个custum“最近的评论”代码,以便在侧边栏中使用。我已经走了很长的路,但只有一件事做不到我想做的。

我想要的是,每页找到的评论中,只显示最后一条评论。我希望post\\u per\\u页面能起到作用,但事实并非如此!

这是我的代码:

<?php

$args=array(
\'author_email\' => \'\',
\'ID\' => \'\',
\'karma\' => \'\',
\'number\' => 5,
\'offset\' => \'\',
\'orderby\' => \'comment_date_gmt\',
\'order\' => \'DESC\',
\'parent\' => \'\',
\'post_id\' => \'\',
\'post_author\' => \'\',
\'post_name\' => \'\',
\'post_parent\' => \'0\',
\'post_status\' => \'publish\',
\'post_type\' => \'page\',
\'posts_per_page\' => 1,
\'status\' => \'approve\',
\'type\' => \'\',
\'user_id\' => \'\',
\'search\' => \'\',
\'count\' => false
);
$comments = get_comments($args);

foreach($comments as $comment) :

?>
<div style="border: 0px solid #000000; height: 1%; overflow: hidden;">
<div style="border: 0px solid #000000; float: left; width: 28%;">
<a href="<?php echo get_author_posts_url($comment->user_id); ?>"><?php echo get_avatar(     $comment, 48 ); ?></a>
</div>
<div style="border: 0px solid #000000; float: right; width: 72%;">
<?php
$aantalcomments = get_comments_number( $comment->comment_post_ID );

?>  
<a href="<?php echo get_author_posts_url($comment->user_id); ?>"><?php echo($comment->comment_author) ?></a> op:<br>


<a href="<?php echo get_comment_link($comment->comment_ID); ?>">

<?php
echo($comment->post_name . " (" . $aantalcomments . ")<br>");
?>
</a>
<?php

echo get_comment_date(\' l, j F, Y \', $comment->comment_ID);

?>
</div>
</div>
<hr>
<?php



endforeach;

?>

2 个回复
SO网友:adrian7

首先,你应该知道所有有评论的帖子:

global $wpdb;

$SelectSQL = "SELECT DISTINCT `comment_post_ID` FROM {$wpdb->comments} LIMIT 0, 15";
$Rows      = $wpdb->get_results($SelectSQL);
然后,针对每个帖子,获取最新评论:

foreach($Rows as $row):
  $comment = get_comments(array(\'post_id\'=>$row->comment_post_ID, \'number\'=>1, \'status\'=>\'appprove\'));
  $comment = $comment[0];

...
并继续显示注释,希望您擅长PHP;)。

另请参见:http://codex.wordpress.org/Function_Reference/get_comments

SO网友:AWK

嗯,我想不出来,但我的兄弟,他非常擅长PHP,确实想出来了。他自己并不认为这是最好的解决方案,但嘿,这是可行的。也许是一个他并不自豪的快速解决方案,但我得救了!;-)他甚至好心地评论了他的工作。我希望其他人也能用这个!

<?php

$args=array(
    \'author_email\' => \'\',
    \'ID\' => \'\',
    \'karma\' => \'\',
    \'number\' => 5,
    \'offset\' => \'\',
    \'orderby\' => \'comment_date_gmt\',
    \'order\' => \'DESC\',
    \'parent\' => \'\',
    \'post_id\' => \'\',
    \'post_author\' => \'\',
    \'post_name\' => \'\',
    \'post_parent\' => \'0\',
    \'post_status\' => \'publish\',
    \'post_type\' => \'page\',
    \'posts_per_page\' => 1,
    \'status\' => \'approve\',
    \'type\' => \'\',
    \'user_id\' => \'\',
    \'search\' => \'\',
    \'count\' => false,
    \'group\' => \'post_id\'
);
$comments = get_comments($args);

// Due to wordpress not having a common functionality, we create a new list of comments manually
$filteredComments = array();
// Posts we already have a comment for
$filteredUsedPosts = array();
// Amount of comments
$filteredCommentLimit = 5;

// Loop through all comments
foreach($comments as $comment) {
    // Have we reached our amount of coments, then weŕe finished
    if (count($filteredComments) < $filteredCommentLimit){
        // Do we already have comment for this post?
        if (!in_array($comment->comment_post_ID, $filteredUsedPosts)){
            // We add the POST ID to the list of used POST IDs
            $filteredUsedPosts[] = $comment->comment_post_ID;
            // We add our comment to the display comments list
            $filteredComments[] = $comment;
        }
    }
}

foreach($filteredComments as $comment) :

    ?>
<div style="border: 0px solid #000000; height: 1%; overflow: hidden;">
    <div style="border: 0px solid #000000; float: left; width: 28%;">
        <a href="<?php echo get_author_posts_url($comment->user_id); ?>"><?php echo get_avatar( $comment, 48 ); ?></a>
    </div>
    <div style="border: 0px solid #000000; float: right; width: 72%;">
        <?php
        $aantalcomments = get_comments_number( $comment->comment_post_ID );

        ?>
        <a href="<?php echo get_author_posts_url($comment->user_id); ?>"><?php echo($comment->comment_author) ?></a> op:<br>


        <a href="<?php echo get_comment_link($comment->comment_ID); ?>">

            <?php
            echo($comment->post_name . " (" . $aantalcomments . ")<br>");
            ?>
        </a>
        <?php

        echo get_comment_date(\' l, j F, Y \', $comment->comment_ID);

        ?>
    </div>
</div>
<hr>
<?php



endforeach;

?>

结束

相关推荐

如何正确使用Comments-template.php

我想修改已登录用户和未登录用户的评论表单。我通过更改注释修改了未登录用户的表单。php,但我不太确定如何为登录用户修改我的表单。我知道我必须使用comments\\u template(),但每当我尝试在页面中使用它时。php,我遇到这样的错误Notice: Undefined variable: args in {PATH}/twwr-theme/comments-template.php on line 13 这些是我评论的内容。php,有点乱<?php if (!empty