【发布时间】:2019-09-26 12:04:06
【问题描述】:
我正在努力完成这项工作,但它并不完全有效。这是我目前查询帖子的方式:
<?php
// the query
$the_query = new WP_Query( array( 'posts_per_page' => -1 ) );
if ( $the_query->have_posts() ) :
?>
<!-- pagination here -->
<!-- the loop -->
<?php
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<li data-href="<?php $zlink = get_the_permalink(); echo preg_replace("#/$#im", '', $zlink);?>">
<div>
<a class="button" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</div>
我想要达到的目标如下:
- 在发布日期较新的帖子之上显示最近评论的帖子...
- 如果博客文章有评论,无论该博客文章有多旧,我都希望该博客文章位于较新的博客文章之上,如果较新的博客文章没有更新(意思是:没有最近的 cmets)。
我首先在顶部显示最近评论的帖子(见下文),但这完全忽略了一个事实,即有些帖子没有 cmets,我找不到将两者结合并显示在一个列表中的方法。
<?php
$args = array(
'status' => 'approve',
'number' => 6,
'order' => 'DESC'
);
$comments = get_comments($args);
foreach($comments as $comment) : $count++;
$post_args = array(
'post_type' => 'post',
'p' => $comment->comment_post_ID,
'posts_per_page' => 1
);
$posts = get_posts($post_args);
foreach($posts as $post) : setup_postdata($post);
the_title();
endforeach;
endforeach;
?>
有人可以帮忙吗?
【问题讨论】:
-
您使用的查询显示最新的 cmets?你到底有什么问题请解释一下?
-
你能明确解释一下你的排序标准吗?示例:如果一篇博文有评论,无论该博文有多旧,我都希望该博文位于较新的博文之上,前提是该博文没有 cmets。
-
没错,@pendo。这是我唯一想要的。所以你可以有 1. old-post-while-has-most-recent-comment-among all-posts 2. latest post 3. old-post-and-not-recently-commented (not before latest post date) 等等在...
-
您能检查一下我发布的解决方案,看看是否满足您的需求?
-
我检查过了,@pendo。请在您的答案下方查看我的反应...
标签: php mysql wordpress post while-loop