Wordpress - Ad Under First Post Only (Adsense)
Dec 6 at 8:08pm by Andrew Benton
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.









3 Responses for "Wordpress - Ad Under First Post Only (Adsense)"
Link
December 13th, 2007 at 12:47 pm
1That’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
Tom Reeves
December 24th, 2007 at 12:33 pm
2Thanks for posting this info. I love your blog! Where’s the RSS button?
alleraJeale
April 13th, 2008 at 2:00 pm
3I’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