2014-04-25 121 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