Subscribe to RSS Feed Login

Andrew Benton

Wednesday
20 August 2008

Wordpress - Ad Under First Post Only (Adsense)

A popular thing to do is to add an Ad (adsense) immediately following the first post on the homepage of a blog. Here is a quick and simple way to do this:

Open index.php under “Presentation > Theme Editor”, it will be titled ‘Main Index Template’

Find this line:

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

Immediately above that, paste this:

<?php
$postnum = 1;
$showadsense1 = 1;
?>

That’s part one of the code - the value.



Staying in index.php, scroll down to this part:

<p class=”postmetadata”>Posted in <?php the_category(’, ‘) ?> | <?php edit_post_link(’Edit’, ”, ‘ | ‘); ?> <?php comments_popup_link(’No Comments »’, ‘1 Comment »’, ‘% Comments »’); ?></p>
</div>
<-Put the below code right here
<?php endwhile; ?>

Into that area between the ending of the div and the endwhile statement place this:

<?php if ($postnum == $showadsense1) {
echo ‘
Your adsense code goes here
‘;
} ?>

<?php $postnum++; ?>

Thats all you have to do! I’ll cover more in-depth ways of inserting ads later this weekend.



Share this article:
These icons link to social bookmarking sites where readers can share and discover new web pages.

  • Reddit
  • Digg
  • NewsVine
  • del.icio.us
  • Technorati
  • Slashdot
  • StumbleUpon
  • Ma.gnolia
  • MisterWong

3 Responses for "Wordpress - Ad Under First Post Only (Adsense)"

  1. Link

    December 13th, 2007 at 12:47 pm

    1

    That’s a good tip, Andrew.

    You could actually simplify it a bit further and eliminate one of those variables by doing something like (pseudo code alert) right inside the post loop:

    if not $adsense then

    $adsense = 1;

    end if

  2. Tom Reeves

    December 24th, 2007 at 12:33 pm

    2

    Thanks for posting this info. I love your blog! Where’s the RSS button?

  3. alleraJeale

    April 13th, 2008 at 2:00 pm

    3

    I’d prefer reading in my native language, because my knowledge of your languange is no so well. But it was interesting! Look for some my links:


RSS feed for comments on this post · TrackBack URI

Leave a reply