How To Display Adsense On Your First Post Within The Loop
The first step is to open up your index.php file in your theme editor. Find the following line:
<?php if(have_posts()) : ?>
Just above that, insert the following like this:
<?php $i = 1; ?>
<?php if(have_posts()) : ?>
Now, scroll down a bit until you find this line:
<?php endwhile; ?>
Insert the following above it, like so:
<?php $i++; ?>
<?php endwhile; ?>
The final step is to insert your Adsense code. Locate where exactly you’d like it within the Loop, and place it between a conditional tag like this:
<?php if ($i == 1) { ?> [YOUR ADSENSE CODE HERE] <?php } ?>
And that’s all you have to do. You can be creative with this code as well. You could add a certain css style class to the top post in your Loop. It doesn’t even have to be the first either, as you can just change the number in $i == 1 to whatever you want. This same method can be used in other archive templates such as archive.php.
Related posts
Comments
Leave a Reply

















