Php 在循环中显示不同的数组元素

Php 在循环中显示不同的数组元素,php,wordpress,Php,Wordpress,我有一个对象数组: Array ( [0] => stdClass Object ( [id] => 24 [ban_id] => 163 [ban_url] => http://www.website.com/wp-content/uploads/2014/04/72890.jpg ) [1] => stdClass Object

我有一个对象数组:

Array
(
    [0] => stdClass Object
        (
            [id] => 24
            [ban_id] => 163
            [ban_url] => http://www.website.com/wp-content/uploads/2014/04/72890.jpg
        )

    [1] => stdClass Object
        (
            [id] => 25
            [ban_id] => 162
            [ban_url] => http://www.website.com/wp-content/uploads/2014/04/46860.jpg
        )

    [2] => stdClass Object
        (
            [id] => 26
            [ban_id] => 169
            [ban_url] => http://www.website.com/wp-content/uploads/2014/04/46871.jpg
        )

)
我还有一个Wordpress循环:

$count = 0;
while ( have_posts() ) : the_post();
   $count++;
   $show_ad = $count%3 == 0;
   if ( $show_ad ):
      echo '<img src="..." alt="" />';
   endif;
endwhile;
或者另一个例子,每3篇文章有2张不同的图片:

[Wordpress POST 1]
[Wordpress POST 2]
[Wordpress POST 3]
   [Image 0]
[Wordpress POST 1]
[Wordpress POST 2]
[Wordpress POST 3]
   [Image 1]
[Wordpress POST 1]
[Wordpress POST 2]
[Wordpress POST 3]
   [Image 2]
[Wordpress POST 1]
[Wordpress POST 2]
[Wordpress POST 3]
   [Image 0]
...
[Wordpress POST 1]
[Wordpress POST 2]
[Wordpress POST 3]
   [Image 0]
   [Image 1]
[Wordpress POST 1]
[Wordpress POST 2]
[Wordpress POST 3]
   [Image 2]
   [Image 0]
[Wordpress POST 1]
[Wordpress POST 2]
[Wordpress POST 3]
   [Image 1]
   [Image 2]
[Wordpress POST 1]
[Wordpress POST 2]
[Wordpress POST 3]
   [Image 0]
   [Image 1]
...

任何帮助都将不胜感激。

在查看您的回复后,我相信回答您问题的最佳方式是为您的横幅广告创建一个增量变量

查看对while循环的以下更改

$count = 0;
$banner_count = 0;
while ( have_posts() ) : the_post();
   $count++;
   $show_ad = $count%3 == 0;
   if ( $show_ad ):
      //I dont know the name of the banner object so i gave it $banner_object
      $banner_url = $banner_object[$banner_count]->ban_url;
      echo '<img src="' .$banner_url .'" alt="" />';

      //Increment Banner
      $banner_count++;

   endif;
$count=0;
$banner_count=0;
while(have_posts()):the_post();
$count++;
$show_ad=$count%3==0;
如果($show_ad):
//我不知道banner对象的名称,所以我给了它$banner\u对象
$banner\u url=$banner\u对象[$banner\u count]->banner\u url;
回声';
//增量横幅
$banner_count++;
endif;
希望有帮助!如果你在找别的东西,请告诉我。如果您只有一定数量的横幅,则可能需要在横幅到达横幅对象末尾时重置横幅计数。请参阅下面的代码

$count = 0;
$banner_count = 0;
while ( have_posts() ) : the_post();
   $count++;
   $show_ad = $count%3 == 0;
   if ( $show_ad ):

      $banner_url = $banner_object[$banner_count]->ban_url;
      echo '<img src="' .$banner_url .'" alt="" />';

      //Increment Banner
      $banner_count++;

      //If reached the end of the banner count
      if($banner_count > count($banner_object)) { $banner_count = 0; }

   endif;
$count=0;
$banner_count=0;
while(have_posts()):the_post();
$count++;
$show_ad=$count%3==0;
如果($show_ad):
$banner\u url=$banner\u对象[$banner\u count]->banner\u url;
回声';
//增量横幅
$banner_count++;
//如果到达横幅计数的末尾
如果($banner\u count>count($banner\u object)){$banner\u count=0;}
endif;

对象代表广告?是的,它们确实代表图像(图像1、图像2、图像3…)您想从对象列表中随机显示一个广告吗?@battletouch事实上,我有一个wordpress页面,其中包含许多帖子(请参见while语句),我想在第三篇帖子之后显示第一个广告,第六篇文章之后的第二个广告,等等…如果我每3篇文章就有2条横幅?我该怎么办?展示3个横幅的条件是什么?所有3个横幅会在while循环的一次迭代中显示吗?是的,它们会在一次迭代中显示。条件是一个变量。例如$nbrBan=1;或$nbrBan=2;或$nbrBan=3;谢谢你给我指出了正确的方向,一切都很好,但我必须将最后一个条件语句从>改为>=否则我会有一个未定义的偏移量错误。没问题!很高兴我能帮忙