PHP: Need loop to alternate between returned posts

别来无恙 提交于 2019-12-05 19:24:49

What if you remove the allqueries stuff and after:

$insightsPosts = new WP_Query($args);

Just use this instead.

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.

while($allqueries[0]->have_posts() || $allqueries[1]->have_posts() || $allqueries[2]->have_posts()) {
  if ($allqueries[0]->have_posts()) $allqueries[0]->the_post();
  if ($allqueries[1]->have_posts()) $allqueries[1]->the_post();
  if ($allqueries[2]->have_posts()) $allqueries[2]->the_post();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!