Title: archive looping
Last modified: August 20, 2016

---

# archive looping

 *  Resolved [markyeoj](https://wordpress.org/support/users/markyeoj/)
 * (@markyeoj)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/archive-looping/)
 * hello guys, I really have this serious problem.
 * here is my code for my archive page
 * [http://pastebin.com/r6BtEUuX](http://pastebin.com/r6BtEUuX)
    and I have three
   widgets for advertisement.
 * What I want is an advertisement after every three posts. I am displaying twelve
   posts per page, so 3 ads every page.. and those 3 ads must came from the 3 widgets
   for ads. Please help regarding this. thanks.

Viewing 8 replies - 1 through 8 (of 8 total)

 *  [David Gard](https://wordpress.org/support/users/duck_boy/)
 * (@duck_boy)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/archive-looping/#post-2411807)
 * Firstly, can you elaborate more on the ‘Should com from the 3 widgets for ads.’
   What are the widgets, how do you connect to them to retrieve the ads?
 * Secondly, I’m no maths wiz, but 12/3 = 4, not 3. Or do you not want one at the
   bottom of the list of posts?
 * My first thought though is that as you are using the The Loop, just add a counter
   to it and include an ad if the number is devisable by 3.
 * Befor The Loop –
 * `$i=0;`
 * After post display in The Loop –
 *     ```
       $i++;
       if($i%3 === 0 && $i <= 9) : // Set $i <= 9 to prevent ad showing at the end of The Loop
           /** Display your add here */
       endif;
       ```
   
 *  Thread Starter [markyeoj](https://wordpress.org/support/users/markyeoj/)
 * (@markyeoj)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/archive-looping/#post-2411816)
 * well, what I want is between the three posts,I dont want to display ads after
   the 12th posts. I already accomplished that by using this code..
 * [http://pastebin.com/gMHLjBvw](http://pastebin.com/gMHLjBvw)
 * the only problem is, It only displays an ads form a single widget. I want every
   ad must came from the 3 widgets.
 *  [David Gard](https://wordpress.org/support/users/duck_boy/)
 * (@duck_boy)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/archive-looping/#post-2411818)
 * Ok, so it’s this that is displaying the add? – `dynamic_sidebar('Header Ad');`
 * If so, and if you have the names for the other 2 widgets, you could put the names
   in to an array before The Loop and call one of them randomly –
 *     ```
       $widget_names = array(
           'Header Ad',
           'Header Ad 2',
           'Header Ad 3'
       );
       ```
   
 * and then –
 * `'dynamic_sidebar($widget_names[rand(0, 2)])`;
 * Or if you wan’t the widgets used in order, add a second counter that increases
   after every 3rd post and use that counter to get the corred widget name.
 *  Thread Starter [markyeoj](https://wordpress.org/support/users/markyeoj/)
 * (@markyeoj)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/archive-looping/#post-2411825)
 *     ```
       <?php
       				$Archive_Ads = array(
           				'Archive Ad1',
           				'Archive Ad2',
           				'Archive Ad3'
       				);
       				if ($i % 3 === 0):
       				if (function_exists('dynamic_sidebar'($Archive_Ads[rand(0, 2)]);) ) :
                       endif;
       				endif;
       				?>
       ```
   
 * whats wrong with my code? it’s not working..
 *  [David Gard](https://wordpress.org/support/users/duck_boy/)
 * (@duck_boy)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/archive-looping/#post-2411842)
 * Try this –
 *     ```
       $Archive_Ads = array(
           'Archive Ad1',
           'Archive Ad2',
           'Archive Ad3'
       );
       if ($i % 3 === 0):
           if (function_exists('dynamic_sidebar') :
               dynamic_sidebar($Archive_Ads[rand(0, 2)]));
           endif;
       endif;
       ```
   
 * Obviously your widgets (whihc I assume are custom?) would have to be called ‘
   Archive Ad1’, ‘Archive Ad2’ and ‘Archive Ad3’
 *  Thread Starter [markyeoj](https://wordpress.org/support/users/markyeoj/)
 * (@markyeoj)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/archive-looping/#post-2411851)
 * sorry bro, I tried to figure it out but still, not yet working
 * Parse error: syntax error, unexpected ‘:’ in /home/pinoytop/public_html/livelong/
   wp-content/themes/livelong/archive.php on line 69
 *     ```
       $Archive_Ads = array(
           'Archive Ad1',
           'Archive Ad2',
           'Archive Ad3'
       );
       if ($i % 3 === 0):
           if (function_exists('dynamic_sidebar') :/*this is the line 69*/
               dynamic_sidebar($Archive_Ads[rand(0, 2)]));
           endif;
       endif;
       ```
   
 *  [David Gard](https://wordpress.org/support/users/duck_boy/)
 * (@duck_boy)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/archive-looping/#post-2411864)
 * I missed a close bracket ‘)’ before the colon ‘:’ – add that and the syntax should
   be ok.
 *  Thread Starter [markyeoj](https://wordpress.org/support/users/markyeoj/)
 * (@markyeoj)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/archive-looping/#post-2412034)
 * Thank you so much!, It works.. 🙂

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘archive looping’ is closed to new replies.

## Tags

 * [looping](https://wordpress.org/support/topic-tag/looping/)
 * [page](https://wordpress.org/support/topic-tag/page/)
 * [posts](https://wordpress.org/support/topic-tag/posts/)
 * [widgets](https://wordpress.org/support/topic-tag/widgets/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 8 replies
 * 2 participants
 * Last reply from: [markyeoj](https://wordpress.org/support/users/markyeoj/)
 * Last activity: [14 years, 5 months ago](https://wordpress.org/support/topic/archive-looping/#post-2412034)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
