我正在努力完成这项工作,但并不完全奏效。这是我目前查询帖子的方式:
<?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>
我要实现的目标如下:
我首先在顶部显示最近发表评论的帖子(请参见下文),但这完全忽略了没有评论的帖子,而且我找不到一种将两者合并并显示在一个列表中的方法。
<?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;
?>
有人可以帮忙吗?
答案 0 :(得分:1)
下面创建了一个空数组,将所有带有注释的帖子添加到该数组。然后,它会添加所有未加评论的帖子。最后,它根据评论日期或发布日期对它们进行排序,具体取决于该帖子是否有评论
//create an array to stuff all your posts in
$arrAllPosts = array();
$args = array(
'post_type' => 'post', //show only posts (not pages, etc)
'comment_count' => array( //pass it an array
'value' => 1, //value is 1, with compare means greater than or equal to 1
'compare' => '>='
),
'posts_per_page' => -1 //gimme all of them
);
$postsWithComments = new WP_Query($args);
while($postsWithComments->have_posts()) {$postsWithComments->the_post();
//get the comments for this post
$comments = get_comments(array(
'post_id' => get_the_ID(), //pass the post ID
'orderby' => 'comment_date', //tell it to sort by the comment date, so you only get the latest
'number' => 1 //just get the latest
));
foreach($comments as $comment) { //we're only looping this once, since there is only one
$arrAllPosts[] = array(
'dateToSortBy' => $comment->comment_date, //we'll use comment date to sort by later, instead of the post date
'the_post_obj' => $post //add the global post object, which is currently set to the current post
);
}
}
//now we get the posts with no comments
$args = array(
'post_type' => 'post', //Only posts (not pages, etc)
'comment_count' => 0, //Posts with no comments only
'posts_per_page' => -1 //gimme all of them
);
$postsNoComments = new WP_Query($args); //run it
while($postsNoComments->have_posts()) {$postsNoComments->the_post(); //loop it
$arrAllPosts[] = array(
'dateToSortBy' => $post->post_date, //we'll use the post date to sort by
'the_post_obj' => $post //add the global post object, which is currently set to the current post
);
}
function date_compare($a, $b) { //create a custom function to sort the array by the date of the post or the date of the comment
$tmA = strtotime($a['dateToSortBy']); //convert to time
$tmB = strtotime($b['dateToSortBy']); //convert to time
return ($tmA < $tmB)?true:false;
}
usort($arrAllPosts, 'date_compare');
//Display the title for each post, from the sorted list
foreach($arrAllPosts as $curPost) {
$post = $curPost['the_post_obj']; //make this the global post object
setup_postdata($post); //setup the data
echo "<a href='" . get_the_permalink() . "'>" . get_the_title() . "</a>";
}