Skip to content

Adding An Advertisement After the First Post on a WordPress Blog

On one of my other sites we run advertising, a banner ad. Because I haven’t had time until today to figure out how to get the ad to appear after the first posting on a page, we’ve been putting the ad on every page of the site, except the home page. The include code is in the footer of the page, so the ad ended up down at the bottom of the page. Not really an optimal place for visibility. So I’ve been digging through the WordPress docs and found the way to do it.

I tried adding an if statement inside the loop, using next_post_link(). But that variable is the same for the entire page. So then I looked for a “first_post” variable, but there isn’t one. So I created one.

In index.php in my template, I added this code:

<?php $ppl=$ppl+1;
if ($ppl==1) include("ad-block.html"); ?>

just after the <p class=”postmetadata”> section, and before the <?php endwhile; ?> section. So now when the $ppl variable is equal to one, the ad-block.html file is included, at other times, nothing is. You could easily use this to have a different text (or included text) show up after different posts. For example, after every second post. Or a different piece of text for the 4th posting on a page.

You can see this on my DC Area Theater information blog,


  1. Jessica says:

    I’m wondering if the include bit could include a php file, is there any reason why not?

    Something like ?

  2. Jessica says:

    Oh sorry, it stripped my code…

    ?php include “bottom.php”; ?

    only with the proper < and > in place

  3. Jessica says:

    It can, I tested it out – just fyi. replace the ad-block.html with say, bottom.php or ads.php and it works great.

    One question – when using the WordPress navigation below the posts on the index.php page (Previous, Next) the include still shows. How would you edit the string to hide itself when someone navigates to, for example?

  4. Hi Jessica, you mean the ad include still shows on page 2? You only want the ads.php or bottom.php to appear after the first post on the main page, not on other pages?