Athena - Responsive Flat WordPress Theme for Personal Blogs and Shops

How to insert ads every two posts in WordPress

It is said that ads inserted on the content perform better than those on sidebars or footers. Let’s see a quick and easy technique for WordPress to display ads every two posts.

In order to do this, we need to initialize a variable before entering The Loop and, during the iteration through the posts, test if its value is even. We will use the Modulus operator %, which in an operation

a % b

is the remainder of a when divided by b. For example:

4 % 2 = 0

3 % 2 = 1

So if the remainder of our variable divided by 2 is zero, we will be at an even post and hence we can insert the ad or anything else. The code is as follows:

$count = 0;
if ( have_posts() ) : while ( have_posts() ) : the_post();
    //before
    if ($count % 2 == 0){
      //do something like displaying an ad
    }
    $count++;
    //after
  endwhile;
endif;

Of course, if you want to insert something every three posts change the number to three. That’s it, have fun inserting!


“How to insert ads every two posts in WordPress” received 26 comments! Add yours.

  1. Empress February 22nd, 2010

    Thanks for this tip…btw, your blog is listed as in the 25 most beautiful blogs: http://www.sitesketch101.com/beautiful-blogs

  2. Elio February 22nd, 2010

    cool, thanks Empress :) You know, if you want to insert an ad only after the first post instead of checking ($count % 2 == 0) you could check ($count == 0) and you’re cool.

  3. ghdhair February 26th, 2010

    an very useful post for me.thank you

  4. spacejams February 26th, 2010

    let me know a lot from this post

  5. You are now listed on FAQPAL March 4th, 2010

    How to insert ads every two posts in WordPress…

    How to insert and display ads every two posts in WordPress using the PHP modulus operator….

  6. Very Useful 65 Wordpress Hacks | Design your way March 8th, 2010

    [...] How to insert ads every two posts in WordPress [...]

  7. Circular Saw Blade April 4th, 2010

    thanks for knowledge

  8. Adam May 30th, 2010

    You could also set it up to be more specific as to where you want the ads to appear using your same count code at the top then using this code within the loop…

    if($count == 1 || $count == 3 || $count == 8){
    //item to be shown here
    }
    $count++;

    Where 3 and 7 specify the post position to display the content after – you can add more OR statements to put it in more positions…

  9. Elio May 30th, 2010

    Correct, Adam. Posibilities are endless. We could also use a switch instruction to display specific chunks of code.

  10. josh October 16th, 2010

    in which php scrip do i have to put that?? in wordpress??

  11. Elio October 18th, 2010

    Yes, the code to display ads every two or more posts must be within WordPress’ Loop, where WP outputs the posts.

  12. Wordpress hacks and tricks | Wordpress Theme Designing, Blog Optimization, Wordpress Videos December 10th, 2010

    [...] How to insert ads every two posts in WordPress [...]

  13. jad March 3rd, 2011

    it didn’t work when i tried to add it

  14. Ed March 4th, 2011

    Hi Elio,

    Would it be possible to update the ads-wordpress-loop post with an example loop containing your code?

    That would really help a lot of people because it would be much clearer how to set this up.

    Thanks!

  15. yash August 9th, 2011

    hey friend i am using this code but it only display 1 post after 2 post

    i want to show ad after every 2nd post

  16. yash August 9th, 2011

    hey friend i am using this code but it only display 1 post after 2 post

    i want to show ad after every 2nd post
    [?php if ($count==2) { ?]
    [div class="clear postborder"> [/div]
    [?php } ?]
    [?php $count = $count + 0; ?]

  17. Elio August 10th, 2011

    Try using the code shown in this post? :D

  18. Very Useful 65 Wordpress Hacks | Stevelizardi.com August 21st, 2011

    [...] How to insert ads every two posts in WordPress [...]

  19. APK Plus December 29th, 2011

    its working! thanks :)

  20. Chris April 18th, 2012

    How do I tie this into this code?

  21. Chris April 18th, 2012

    Opps code didnt post… sorry

    [?php $myCats = array(63,86); // Excluded Categories
    query_posts($query_string); // Requested variables
    if(have_posts()) :while (have_posts()) : the_post();
    if(in_category($myCats)) continue;
    ?]

  22. Jluisfg May 24th, 2012

    Really helpful! Such a simple but powerful solution, thanks a lot!

  23. Funny Pictures March 7th, 2013

    This worked flawlessly on my website.

    Regards

  24. Tarangkhandelwal October 8th, 2013

    In which php file under WordPress theme do i need to use this loop???

  25. Elio October 24th, 2013

    You can do it on index.php for example, or any archive template in general, like category.php, archive.php and so on. That is, of course, assuming your theme complies with WordPress code standards.

  26. Very Useful 65 WordPress Hacks » Stevelizardi.com November 9th, 2013

    […] How to insert ads every two posts in WordPress […]

Leave a comment