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:
[php]
$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;
[/php]
Of course, if you want to insert something every three posts change the number to three. That’s it, have fun inserting!
Thanks for this tip…btw, your blog is listed as in the 25 most beautiful blogs: http://www.sitesketch101.com/beautiful-blogs
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.
an very useful post for me.thank you
let me know a lot from this post
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….
[…] How to insert ads every two posts in WordPress […]
thanks for knowledge
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…
Correct, Adam. Posibilities are endless. We could also use a switch instruction to display specific chunks of code.
in which php scrip do i have to put that?? in wordpress??
Yes, the code to display ads every two or more posts must be within WordPress’ Loop, where WP outputs the posts.
[…] How to insert ads every two posts in WordPress […]
it didn’t work when i tried to add it
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!
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
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; ?]
Try using the code shown in this post? 😀
[…] How to insert ads every two posts in WordPress […]
its working! thanks 🙂
How do I tie this into this code?
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;
?]
Really helpful! Such a simple but powerful solution, thanks a lot!
This worked flawlessly on my website.
Regards
In which php file under WordPress theme do i need to use this loop???
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.
[…] How to insert ads every two posts in WordPress […]