Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:需要循环在返回的帖子之间切换_Php_Wordpress_Merge_Alternate - Fatal编程技术网

PHP:需要循环在返回的帖子之间切换

PHP:需要循环在返回的帖子之间切换,php,wordpress,merge,alternate,Php,Wordpress,Merge,Alternate,我有一个通过执行三个查询返回的帖子数组。 来自博客的3篇文章,这些文章不在“媒体”或“见解”中, 3来自“媒体”中的博客 3从“见解”中的博文中 这是我要的。我认为这不是最优雅的解决方案: <? $args = array( 'post_type' => 'post', 'posts_per_page' => 3, 'category__not_in' => array( 268, 269 ) ); $homePosts = new WP_Qu

我有一个通过执行三个查询返回的帖子数组。 来自博客的3篇文章,这些文章不在“媒体”或“见解”中, 3来自“媒体”中的博客 3从“见解”中的博文中

这是我要的。我认为这不是最优雅的解决方案:

<? $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帖子

我需要的是循环通过1个homepost,1个In-Media post,1个bt insight post,然后1个homepost,1个In-Media post。。。等等,重复一遍


希望这是有道理的。建议?

如果删除allqueries内容,然后:

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();
}
$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;
}
($i=0;$i<3;$i++)的
{
如果($homePosts->post\u count>$i)
echo$homePosts->posts[$i]->post_title;
如果($inthemediaPosts->post\u count>$i)
echo$inthemediaPosts->posts[$i]->post\u title;
如果($insightsPosts->post_count>$i)
echo$insightsPosts->posts[$i]->post_title;
}

这应该只是打印出文章的标题,您可以根据需要使用其他字段。可以将其移动到函数中,以避免重复逻辑。

请查看本问题中描述的自定义用户函数,这将允许您合并三个不同的数组,交替使用每个数组的值。这种方法将足以满足您的需求achieve@Scuzzy,
$myquery
似乎不是一个数组,但是…我假设可以完成以下操作:foreach(数组\u zip\u merge($homePosts,$inthemediaPosts,$insightsPosts)作为$myquery)查看WordPress抄本,您可能需要提前编译三个单独的帖子数组,然后再进行合并。(很抱歉,我无法再编辑以前的评论)