Support » Themes and Templates » Adding ads and stuff between posts

  • In index.php I am using:
    <?php if ($count == 1) : ?> content <?php endif; $count++; ?>

    To insert an adsense banner between the first and second post. But now I would like to control the space between the first and second post, as well as between other posts (such as the second and third post).

    I tried using:
    <?php if ($count == 2) : ?> content <?php endif; $count++; ?>

    But it didn’t work. Can someone please post the correct code for inserting things in between other posts?

Viewing 12 replies - 1 through 12 (of 12 total)
  • if you added both codes, and unless you put it into a weird location within your index.php, there is no obvious reason why this should not work.
    (assuming that $count is set to 1 before the loop)

    you could paste the whole code of index.php into a http://wordpress.pastebin.com/ and post the link to it here.

    It is displaying the banners but how it displays is in this order:

    1. 1st post excerpt
    2. banner 1
    3. banner 2
    4. 2nd post excerpt
    5. 3rd post excerpt

    try to add the code in this order:

    <?php if ($count == 2) : ?> content <?php endif; $count++; ?>
    <?php if ($count == 1) : ?> content <?php endif; $count++; ?>

    Hey that worked!

    But why?

    logic behind it:
    – $count starts with 1;
    – it passes the first if-statement ($count == 2) , which is not true, so does nothing;
    – it passes the second if-statement ($count == 1), which is true, so output content, increse $count, which is now 2;
    -loop;
    – now $count equals 2;
    – it passes the first if-statement ($count == 2) , which is now true, so output content, increse $count, which is now 3;
    – it passes the second if-statement ($count == 1), which is now not true, so do nothing;
    -loop;
    -from now $count equals 3, and further both if-statements are nor true.

    Now it makes sense. Thank you very much for walking me through this!

    One Line?

    <?php if($count==1 || $count==2) : ?> content <?php endif; $count++; ?>

    David

    How would that work if it is two different ads?

    You said adSense and as adSense will deliver a different add each call, I was assuming that these are two banner ads (Adsense max 3 on any page).

    loop 1 $count == 0;
    No Ad $count out of range increment count++;
    1st ‘Post’ output

    Loop 2
    $count == 1; Output ‘Call Banner’ increment count++;
    2nd ‘Post’ output

    Loop3
    $count == 2; Output ‘Call Banner’ increment count++;
    3nd ‘Post’ output

    loop 4;
    No Ad $count out of range increment count++;
    4th ‘Post’ output

    Second and Fourth and sixth posts
    <?php if($count < 7 && !$count % 2) : ?> content <?php endif; $count++; ?>

    HTH

    David 😉

    @adeptris i said adsense as an example (bad example i guess), thank you for the reply.

    I am trying to add a third slot now and it won’t show up. I still can’t figure out the logic of why this won’t work:

    <?php if ($count == 3) : ?> content <?php endif; $count++; ?>
    <?php if ($count == 2) : ?> content <?php endif; $count++; ?>
    <?php if ($count == 1) : ?> content <?php endif; $count++; ?>

    The results I was aiming for: 1 would go between post 1 and 2; 2 would go between posts 2 and 3; 3 would go between posts 3 and 4.

    Isn’t that how it should work?

    i would say as well – famous words – it ‘should’ work.

    you could try a different approach, with the counter increment outside of the if/elseif statements:

    <?php if ($count == 1) : ?> content1
    <?php elseif ($count == 2) : ?> content2
    <?php elseif ($count == 3) : ?> content3
    <?php endif;
    $count++; ?>

    Hi Neononcon,
    The counter is just after the loop so it is set for all elements in that iteration.

    <?php while ( have_posts() ) : the_post(); ?>
    
    <?php /* increment the counter */ ?>
    <?php $count+=1; ?>

    An Alterantive Way
    Now you could make this really simple, and just create template parts, which are php files, call these ‘advert hyphon index’ like advert-1.php advert-3.php

    Now all you need to do is in the loop add one comment and a code line.

    <?php /* increment the counter */ ?>
    <?php $count+=1; ?>
    
    <?php /* Get the advert by index if exists */ ?>
    <?php get_template_part( 'advert', $count ); ?>

    Now how cool, clean and tidy is that, include_file() would error if the file was missing, get_template_part(), as I understand it, will not error because if it cannot find the file it returns nothing, it uses locate_template() which will return nothing, I have not tested this so feedback if you try it.

    Working Example of a Template Part
    Adding navigation-2.php

    <?php /* Start add our second page menu */ ?>
    <div id="pagemenu" role="navigation">
    	<?php wp_nav_menu( array( 'container_class' => 'page-header', 'menu_class' => 'page-menu', 'theme_location' => 'secondary', 'depth' => 0, 'fallback_cb' => '' ) ); ?>
    </div>
    <?php /* End lower page menu */ ?>

    Called in header.php

    <?php /* Get our second navigation bar */ ?>
    <?php get_template_part( 'navigation', 2 ); ?>

    HTH

    David 🙂

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Adding ads and stuff between posts’ is closed to new replies.