gpt4 book ai didi

wordpress - 仅显示作者的最新评论

转载 作者:行者123 更新时间:2023-12-02 03:38:52 24 4
gpt4 key购买 nike

您好,我有代码可以在我的模板中显示评论列表。我的问题可能只显示作者的最新评论。示例:如果作者 B 在同一篇文章中发表了 3 条评论,我想只显示作者 B 的最新评论并排除/隐藏作者 B 发表的其他评论。

这里是我的代码:

    <table class="table table-striped">  
<thead class="btn-primary bottom-margin">
<tr>
<th>Form ID</th>
<th>Subject</th>
<th>Author</th>
<th>Date</th>
</tr>
</thead>

<tbody>

<?php $args = array(
'status' => 'approve',
'number' => 0,
'order' => 'DESC'
);
$comments = get_comments($args);
foreach($comments as $comment) : $count++;?>

<?php $post_args = array(
'post_type' => 'ticket_system',
'p' => $comment->comment_post_ID,
'posts_per_page' => 50
);

$posts = get_posts($post_args);
foreach($posts as $post) : setup_postdata($post);?>

<tr>
<td class="col-md-2 flags"><?php echo get_post_meta($post->ID, "idticket",$single = true); ?></td>
<td class="col-md-6 flags"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title();?></a></td>
<td class="col-md-2 flags"><?php echo $comment->comment_author;?></td>
<td class="col-md-2 flags"><?php echo $comment->comment_date;?></td>
</tr>


<?php endforeach;
endforeach; ?>

</tbody>
</table>

最佳答案

您必须使用“GROUP BY”子句从每个作者那里获取最新的评论,并且函数“get_comments()”不接受“group by”的参数。 (支持的参数列在 here 中)。

您必须通过 get_results() 函数查询评论表并传递 MySql 查询,因此代码将如下所示:

$table_prefix = $wpdb->prefix; // Get the WP table prefix in your database
$table_name = "comments"; // Table name we need to query
$table = $table_prefix . $table_name; // prefixing the table name
$comments = $wpdb->get_results("SELECT * FROM (SELECT * FROM " . $table . " where comment_approved=1 order by comment_date desc)c group by comment_author order by comment_date desc");

现在在 $comments 中你有每个作者的最新评论,你可以随意循环。

请注意,我按作者姓名对评论进行分组,您可以对“comment_author_email”进行分组,或者按“comment_author_IP”进行非常具体的分组。

希望对您有所帮助。

关于wordpress - 仅显示作者的最新评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21495845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com