Written on October 7, 2008 at 2:14 pm by Chase Sagum

Placing an Ad Between 1st & 2nd Post – WordPress

CMS, Web Development one comment

Okay i’ve got a new one for you! What if you need to place a banner ad between the 1st and 2nd blog posts only in the WordPress “Loop“? How exactly would you go about doing that? I recently had a client ask me for this feature and I was not sure at all how to do it. Thanks to Google and some PHP goods here is how I got it done…

Here’s what your WordPress Loop basically looks like in your index.php or archive.php files etc.

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<!-- do stuff -->
<?php endwhile; ?>
<?php endif; ?>

The following code will help you place an ad between the 1st and 2nd blog posts in your WordPress Loop:

<?php
$postcounter = 1; // set counter to 1 for ads
if (have_posts()) : ?>
<?php while (have_posts()) : $postcounter = $postcounter + 1;

the_post(); ?>
<!-- do stuff -->
<?php if(2 == $postcounter) {
echo
'
<div id="adsbetween">
<-- Place ad code here -->
</div>
'
; } ?>
<?php endwhile; ?>
<?php endif; ?>

Try that. That should work just fine for you! You can learn more here at this url. http://www.nzwhost.com/article/placing-adsense-between-posts

one response to " Placing an Ad Between 1st & 2nd Post – Wordpress"

  1. Paul W. on October 20, 2008:

    I just stumbled across your blog, and really appreciate your in depth posts. I feel a lot of time it’s hard to get the meat from most web development blogs. So, here’s my question for you. I have seen a growing trend for people to offer free SEO wordpress themes (see here and here). I understand how to add plugins to make the theme more SEO friendly, but I am curious what makes a theme SEO-ed right out of development. Any insights would be appreciated. ~Paul

Leave a comment