2014-04-25 72 views
0

我有对象的数组:显示不同的数组元素

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; 

我想显示一个(或甚至更多,取决于如果$ show_ad等于true(在这种情况下,每3个帖子),这些图像就会被用户选择)。

例如,1幅不同的图像,每3个帖子:

[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] 
... 

或者另一实例中,2个不同的图像,每3个帖子:

[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] 
... 

任何帮助将不胜感激。

+0

对象代表广告? –

+0

是的,他们确实代表图片(图片1,图片2,图片3 ...) – roastedtoast

+0

您想要显示对象列表中的随机广告吗? –

回答

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; 

希望有帮助!让我知道你是否在寻找别的东西。如果您只有一定数量的横幅,那么您可能需要在横幅广告对象结束时重置banner_count。请参阅下面的代码

$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; 
+0

如果我有两个横幅显示每3个职位?我能怎么做? – roastedtoast

+0

显示3条横幅的条件是什么?并且所有3个横幅会在while循环的一次迭代中显示? –

+0

是的,他们将在一次迭代中显示。条件是一个变量。例如$ nbrBan = 1;或$ nbrBan = 2;或$ nbrBan = 3; – roastedtoast