Title: Insert Ads between posts and random groups
Last modified: August 21, 2016

---

# Insert Ads between posts and random groups

 *  [Pancho Perez](https://wordpress.org/support/users/lonchbox/)
 * (@lonchbox)
 * [12 years, 7 months ago](https://wordpress.org/support/topic/insert-ads-between-posts-and-random-groups/)
 * I found a simple way to insert ads inside a Loop, and also rotate between two
   Groups.
 *     ```
       <?php
           $query = new WP_Query(array(
               'post_type' => array('post'),
   
           ));
       ?>
       <?php if ( $query->have_posts() ) : ?>
           <?php $_count = 1; ?>
       	<?php while ( $query->have_posts() ) : $query->the_post(); ?>
       		<?php get_template_part( 'partials', 'content' ); ?>
   
       		<?php if ($_count == 1) : ?>
       			<?php echo adrotate_group(rand(2,3)); ?>
       		<?php endif; $_count++; ?>
   
       	<?php endwhile; ?>
       <?php endif; ?>
       ```
   
 * As you see in the code, the banners use the `$_count` to position the `adrotate_group()`,
   in this example the banners show in the second position.
    To show random two 
   groups I use `rand(2,3)` inside the adrotate function where 2 & 3 are the ID 
   of the groups I want to show in that position.
 * Hope it helps 🙂
 * [http://wordpress.org/plugins/adrotate/](http://wordpress.org/plugins/adrotate/)

The topic ‘Insert Ads between posts and random groups’ is closed to new replies.

 * ![](https://ps.w.org/adrotate/assets/icon-256x256.gif?rev=3117289)
 * [AdRotate Banner Manager](https://wordpress.org/plugins/adrotate/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/adrotate/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/adrotate/)
 * [Active Topics](https://wordpress.org/support/plugin/adrotate/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/adrotate/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/adrotate/reviews/)

## Tags

 * [count](https://wordpress.org/support/topic-tag/count/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * 0 replies
 * 1 participant
 * Last reply from: [Pancho Perez](https://wordpress.org/support/users/lonchbox/)
 * Last activity: [12 years, 7 months ago](https://wordpress.org/support/topic/insert-ads-between-posts-and-random-groups/)
 * Status: not a support question