73 posts
    Has been part of the Envato Community for over 5 years Has referred 1+ members Has collected 10+ items on Envato Market Sells items exclusively on Envato Market
+1 more
Nominata says

Hi,

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

Cheers

741 posts
    Has referred 10+ members Has sold $40,000+ on Envato Market Has collected 50+ items on Envato Market Had an item featured on Envato Market
+4 more
Orbital_Themes says

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!

73 posts
    Has been part of the Envato Community for over 5 years Has referred 1+ members Has collected 10+ items on Envato Market Sells items exclusively on Envato Market
+1 more
Nominata says

hmm I get what you mean but could you give a fuller example?

So if I did:

$homead++

if {$homead==4

then what? Sorry bit rusty on this

It is helping but could you explain abit more ?

Thanks

962 posts
    Has been part of the Envato Community for over 5 years Has sold $10,000+ on Envato Market Has collected 100+ items on Envato Market Sells items exclusively on Envato Market
+1 more
EireStudio says
<?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; ?>    

   ...

73 posts
    Has been part of the Envato Community for over 5 years Has referred 1+ members Has collected 10+ items on Envato Market Sells items exclusively on Envato Market
+1 more
Nominata says

Not working as of yet – I noticed there was already a var for the counter so I didnt add it again. I just added some code at the bottom of the end while

http://pastie.org/786246

741 posts
    Has referred 10+ members Has sold $40,000+ on Envato Market Has collected 50+ items on Envato Market Had an item featured on Envato Market
+4 more
Orbital_Themes says
<?php $counter = 0; ?>    

<?php while(have_posts()) : the_post(); ?>

      <?php $counter = $counter + 1; ?>    

      
// Post Content <?php if($counter % 2 == 0) : ?> // Banner <?php endif; ?> ...

Exactly :)

73 posts
    Has been part of the Envato Community for over 5 years Has referred 1+ members Has collected 10+ items on Envato Market Sells items exclusively on Envato Market
+1 more
Nominata says

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 :)

123 posts
    Has been part of the Envato Community for over 4 years Has referred 1+ members Has sold $1,000+ on Envato Market Has collected 1+ items on Envato Market
+2 more
adam_robinson says

what about


  <?php if($counter  == 4) : ?>  

            // Banner

           <?php $counter = 0;    ?>

  <?php endif; ?>  

73 posts
    Has been part of the Envato Community for over 5 years Has referred 1+ members Has collected 10+ items on Envato Market Sells items exclusively on Envato Market
+1 more
Nominata says

No sorry didnt work, Pogo has being the closest. I think its how the template has being made.

If you look here this is the full code http://pastie.org/786246

Want to add some code after every 2 posts.

741 posts
    Has referred 10+ members Has sold $40,000+ on Envato Market Has collected 50+ items on Envato Market Had an item featured on Envato Market
+4 more
Orbital_Themes says

I assume that the “HelloHelloHello(...)” part is yours – delete it.

Put this before endwhile:

<?php if($counter2 % 2 == 0) : ?>
    // your banner code goes here
<?php endif; ?>
by
by
by
by
by
by