How to add Adsense Ad Unit only after First Post in WordPress Blog?

I always wanted to show an Adsense ad unit below the first post in my blogs. I only show summaries of posts on the home page in order to prevent duplicate content issue. So I thought that showing an ad unit below the first post’s summary will be good.

I tried to find a solution but I wasn’t very lucky. I tried few WordPress plugins but failed to find what I was looking for. Then I searched the internet for any possible solution. I finally got my answer after few hours of search on the internet.

Here is what you need to do in order to display an ad unit on your home page after only the first post.

Open the Index.php page of the theme that you are using [download it from your server if you don't have it in your PC].

Now look for this function in the index.php file

the_excerpt

Or this function

the_content

The first function, which is [the_excerpt], is used if you want to show only summary of posts on your blog’s home page.

Where the function, the_content, is used if all the content of the post is displayed on the home page.

Below the line where this function ends, you will have to add this code.

<?php if(is_home() && $post==$posts[0] && !is_paged()) { ?>

Put Adsense Ad Code Here

<?php } ?>

Save the file and then upload it. You will now see an ad unit below the first post [only] on our home page.

  1. WordPress: How to remove Elements from the Head Section: Via Editing the Core Files?
  2. How to Remove CleverPlugins Advertisement/News from WordPress Dashboard
  3. WordPress: Cannot get rid of All
  4. WordPress: How to remove the Index, Start, Next, Prev Link Elements
  5. WordPress: How to remove the Shortlink Link Element

2 Responses to How to add Adsense Ad Unit only after First Post in WordPress Blog?

  1. Shafi

    I got AdSense code from Google. it gives me error. Because the code itself is not php code.
    How do I modify the AdSense code for php.

  2. You do not have to modify anything. You just have to open index.php file and paste in where you want the ad to appear. But remember but to place the code inside PHP section.

    That is: you do not paste the ad code in between < ?php and ?>

    If you still have problems then can you paste here the part of code in your file which contains the ad code?

*