WP: Inserting ads after posts on your WP index

advertising blog stuff tech

This is a very basic guide to inserting ads in your blog’s home page, please keep in mind that I’m not an expert in PHP, so if somebody has a better way to do this let me know.

Now, the reason I’m posting this is because, while there are many tutorials that show you how to insert one ad after the first post (or the second one, and so on), I haven’t really found a recent guide to placing one ad after X post and another after Y post.

So, as I said I’m no expert, but I was able to insert an ad after the first post and another one after the 7th post in my index.php, so let the code raping begin!

WARNING: MAKE A BACKUP OF THE FILE YOU’RE ABOUT TO EDIT, also if you’re not comfortable editing code on your blog please STOP right here, I make no promises or take any responsibility for whatever damage you do to your website. I mean it ¬¬.

Inserting an ad after first post

On your theme, open up either index.php, loop.php, or the php file that contains the following:

<?php if (have_posts()) : ?>

Paste this right above it:

<?php $count = 1; ?>

Now look for this bit of code:

</div>

<?php endwhile; ?>

And insert the next piece of code in the space between </div> and <?php endwhile; ?>:

<?php if ($count == 1) : ?>
<div>
==> INSERT CODE HERE <==
</div>
<?php endif; ?>
<?php $count++; ?>

So it now looks like this:

</div>
<?php if ($count == 1) : ?>
<div>
==> INSERT CODE HERE <==
</div>
<?php endif; ?>
<?php $count++; ?>
<?php endwhile; ?>

The way it works is this, <?php if ($count == 1) : ?> means that if  the counter equals 1 the ad will show up after the first post on your front page, if equals 2 it will show after the second, 3 after the third, and so on, for example:

</div>
<?php if ($count == 3) : ?>
<div>
==> INSERT CODE HERE <==
</div>
<?php endif; ?>
<?php $count++; ?>
<?php endwhile; ?>

That means the ad will show up under the third post on your index page, please keep in mind that the only number you have to change is the one in <?php if ($count == 3) : ?>, DON’T touch the one in the first bit of code, <?php $count = 1; ?> , only the one with the “if” statement.

The <div> you see around the “INSERT CODE HERE” is just to align your ad, either by CSS (<div class=”something”>) or by just centering (<div align=”center”>) or whatever you want to do with it.

via

Inserting a second ad after another post

The code above works great, but I wanted to ad an extra ad under another post, in my case I wanted an ad under the first post and another one under the 7th post on the front page.

I’m not very good at php and didn’t want to spend too much time looking around for a complicated way to deal with this, so after a long session of trial and error I think I got it to work.

This is what I did, I inserted this piece of code right after the first ==> INSERT CODE HERE <==
</div>,
but before <?php $count++; ?>:

<?php if ($count == 7) : ?>
<div>
==> INSERT CODE HERE <==
</div>
<?php endif; ?>

So in my case it looks like this:

</div>

<!-- ad block under first post -->
<?php if ($count == 1) : ?>
<div>
==> INSERT CODE HERE <==
</div>
<?php endif; ?>
<!-- end ad block under first post -->

<!-- ad block under seventh post -->
<?php if ($count == 7) : ?>
<div>
==> INSERT CODE HERE <==
</div>
<?php endif; ?>
<!-- end ad block under seventh post -->
<?php $count++; ?>

<?php endwhile; ?>

Note that there’s only one <?php $count++; ?>, so make sure you only have one of those and if your site doesn’t load double check you didn’t leave any tags open.

I don’t recommend trying to insert an ad after every post on your index, mainly because you might get kicked out of whatever ad thing you use, for me 3 ads on the front page (2 in between posts and one on the sidebar) are more than enough, I don’t want too many ads and slow down my site.

Does it work better? I don’t know, maybe, to be honest I don’t get a ton of visitors so I only see pennies once in a while, might work better on a site with hundreds and thousands of visitors a day, but it doesn’t hurt to try, besides I just added the second ad between posts a few hours ago.

To be honest I don’t know if this is right, I’m sure there is a better way to do it, probably some kind of boolean code or a some kind of while statement, as I said before if somebody has a better way to do it please let me know so I can clean up my code.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.