On my wordpress blog I want to be able to add an Ad or a section after say every 2 or every 4 posts.
How would I be able to do this? I have had a look in the loop but anything I tried didnt work.
Hope you can help
It easy – just set up a variable that equals 0. Now at the beginning of the loop add +1 to the variable
$your_var++. At the end of the loop, after the post, make an if statement that checks if $your_var is dividable by four (or two or whatever you want). If yes – put something special there.
Hope this helps!
hmm I get what you mean but could you give a fuller example?
So if I did:
then what? Sorry bit rusty on this
It is helping but could you explain abit more ?
<?php $counter = 0; ?> <?php while(have_posts()) : the_post(); ?> <?php $counter = $counter + 1; ?> <div class="post"> // Post Content </div> <?php if($counter % 2 == 0) : ?> // Banner <?php endif; ?> ...
<?php $counter = 0; ?> <?php while(have_posts()) : the_post(); ?> <?php $counter = $counter + 1; ?>// Post Content <?php if($counter % 2 == 0) : ?> // Banner <?php endif; ?> ...
Cool I got it working .. but its showing after every 2 posts all the time. Even if I change the vaule to
$counter % 4 == 0
Would I have control of when to show it? say by every 6 / 8 / or how every many posts I want?
Thanks by the way to both of you
<?php if($counter == 4) : ?> // Banner <?php $counter = 0; ?> <?php endif; ?>