0

I have an array of objects:

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
        )

)

I also have a Wordpress loop:

$count = 0;
while ( have_posts() ) : the_post();
   $count++;
   $show_ad = $count%3 == 0;
   if ( $show_ad ):
      echo '<img src="..." alt="" />';
   endif;
endwhile;

I would like to display one (or even more, depends on the user's choice) of the images if $show_ad is equal to true (every 3 posts in this case).

For example, 1 different image every 3 posts :

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

Or another example, 2 different images every 3 posts :

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

Any help will be greatly appreciated.

4
  • The objects represent the ads? Commented Apr 25, 2014 at 12:28
  • Yes they do represent the images (Image 1, Image 2, Image 3...) Commented Apr 25, 2014 at 14:07
  • Would you like to show a random ad from your list of objects? Commented Apr 25, 2014 at 14:39
  • @battletoilet in fact, I have a wordpress page that contains a number of posts (see the while statement), I want to show the first ad after the 3rd post, the 2nd ad after the 6th post and so on... Commented Apr 25, 2014 at 15:53

1 Answer 1

1

After reviewing your response, i believe the best way to answer your question is to create an incremental variable as well for your banner ads.

Review the following changes to your while loop.

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

Hope that helps! Let me know if your looking for something else. If you only have a certain amount banners, then you may want to reset banner_count when it reaches the end of your banners object. See code below

$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;
Sign up to request clarification or add additional context in comments.

4 Comments

And if I have 2 banners to show every 3 posts? how can I do?
Whats the condition for showing 3 banners? And will all 3 banners display in one iteration of the while loop?
Yes they will display in one iteration. The condition is a variable. For example $nbrBan = 1; or $nbrBan = 2; or $nbrBan = 3;
Thanks for putting me in the right direction, everything works well but I had to change the last conditional statement from > to >= otherwise I had an undefined offset error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.