Skip to main content
WordPress made easy with the drag & drop Total WordPress Theme!Learn More
Tutorials

Insert Adsense After Your First Post In WordPress

May 17, 2011
How To Insert Google Adsense

Anyone that has some experience with Adsense knows how well ad spots convert when placed within your content. In today’s daily tutorial I’ll show you how to easily add an adsense spot (or virtually anything else) right after your first post in a WordPress loop, such as your homepage or category page.

Create A Counter For Your Loop

This method works by adding a PHP counter within your loop that will count up as posts are retrieved from your database. So starting from 0 as soon as it finds the first post it will ad your adsense spot above but it won’t do it for all the other posts.

1. Add counter before “while” Loop & Start Counting After

<?php
// Define count variable
$count = 0;

// Start your standard WP loop (this should already be part of your theme)
while ( have_posts() ) : the_post();
    
    // Increase counter for each post
    $count++;

2. Set Up The Conditional Inside The Loop

// Show ad if post count is 1
if ( $count == 1 ) { ?>
    Your Adsense Code
<?php }

3. Full Example of Loop with Added Code

// Define count variable
$count = 0;

// Start your standard WP loop (this should already be part of your theme)
while ( have_posts() ) : the_post();
    
    // Increase counter for each post
    $count++;

    // Show ad if post count is 1
    if ( $count == 1 ) { ?>
        Your Adsense Code
    <?php }

    // Whatever you want to add inside the loop here
    // Or...whatever is already in your theme

// End loop
endwhile;

Insert Adsense With A Plugin

Google Adsense Free WordPress Plugin

An alternative is to insert Adsense into your posts and pages with a plugin. One option is the free Google Adsense plugin which was made by Google and is the official Adsense plugin in the WordPress plugin repository. This plugin does only have a 2.8 rating but from reading the reviews the low ratings seem to be due to a lack of placement options. So if you want a simple plugin to insert your ads this might be the right plugin for you.

WP Simple Adsense Insertion Free WordPress Plugin

A better option might be the free WP Simple Adsense Insertion plugin. This lovely little plugin currently has a 4.6 rating and makes it easy to insert ads (Adsense or other ad code too) into your posts, pages and sidebars using a simple shortcode.

Subscribe to the Newsletter

Get our latest news, tutorials, guides, tips & deals delivered to your inbox.

Comments

No comments yet. Why don't you kick off the discussion?

Leave a Reply

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

Learn how your comment data is processed by viewing our privacy policy here.