user891988
user891988

Reputation: 1

How do I create ad space after every nth post?

I want to put ad code after every 4th post in my home page, how to do that (http://www.news.shreshthbharat.in/bharat)

Here is pastbin code http://pastebin.com/3vzhZKxE

I am using this code right now:

`<?php if ($count==3) { ?>
<div class="clear"> </div>
<--- ad code or widget area- - - > 
    <div class="clear"> </div>
<?php } ?>
<?php $count = $count + 0; ?>`

with this code, it displays ad after 2nd post.

Upvotes: 0

Views: 691

Answers (1)

Chris Carson
Chris Carson

Reputation: 1845

Try this...

$count = 0;
if (have_posts()) : while (have_posts()) : the_post(); 
    $count++;
    $show_ad = $count % 4 == 0;
    //output the post
    //show the ad after the post

The % is the modulus operator. It returns the remainder of a/b. The loop would work like this...

EDITED TO CORRECT FAULTY MATH

$count = 1, 1 % 4 = 1, so $show_ad = false
$count = 2, 2 % 4 = 2, so $show_ad = false
$count = 3, 3 % 4 = 3, so $show_ad = false
$count = 4, 4 % 4 = 0, so $show_ad = true
etc...

Upvotes: 1

Related Questions