PHP:Need循环在返回的帖子之间交替 [英] PHP: Need loop to alternate between returned posts
问题描述
我有一系列的帖子,它们通过执行三个查询而返回. 博客中有3篇帖子不在在媒体中"或见解"中, 博客中的3个,其中的帖子位于媒体"中 博客中的3个,其中的帖子位于洞察"中.
I have a an array of posts that are returned by doing three queries. 3 posts from the blog where posts are NOT in 'In the Media' or 'Insights', 3 from the blog where posts are in 'In the Media' 3 from the blog where posts are in 'Insights'.
这就是我要的.我认为这不是最优雅的解决方案:
Here's what I have for that. I don't think it's the most elegant solution:
<? $args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'category__not_in' => array( 268, 269 )
);
$homePosts = new WP_Query($args);
$args = array(
'post_type' => 'post',
'category_name' => 'in-the-media',
'posts_per_page' => 3
);
$inthemediaPosts = new WP_Query($args);
$args = array(
'post_type' => 'post',
'category_name' => 'bt-insights',
'posts_per_page' => 3
);
$insightsPosts = new WP_Query($args);
$allqueries = array($homePosts,$inthemediaPosts,$insightsPosts);
foreach ($allqueries as $myquery) {
while ($myquery->have_posts()) : $myquery->the_post(); ?>
当前,该循环遍历3个家庭帖子,然后是3个媒体帖子,然后是3个bt-insight帖子.
Currently, that loops through 3 homeposts, then 3 inthemedia posts, then 3 bt-insight posts.
我需要的是让循环遍历1个homepost,1个inthemedia post,1个bt-insight post,然后1个homepost,1个inthemediea post ...等等重复.
What I need is for the loop to go through 1 homepost, 1 inthemedia post, 1 bt-insight post, then 1 homepost, 1 inthemediea post... etc repeat.
希望如此.有建议吗?
推荐答案
如果在之后删除allqueries内容怎么办?
What if you remove the allqueries stuff and after:
$insightsPosts = new WP_Query($args);
只需使用它即可.
for ($i = 0; $i < 3; $i++) {
if ($homePosts->post_count > $i)
echo $homePosts->posts[$i]->post_title;
if ($inthemediaPosts->post_count > $i)
echo $inthemediaPosts->posts[$i]->post_title;
if ($insightsPosts->post_count > $i)
echo $insightsPosts->posts[$i]->post_title;
}
那应该只打印出帖子的标题,您可以根据需要使用其他字段.可以将其移至一个函数中,以避免重复逻辑.
That should just print out the title of the post and you can use the other fields as needed. This could be moved to a function to avoid duplicating the logic as well.
这篇关于PHP:Need循环在返回的帖子之间交替的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!