How to add Adsense after the first post in WordPress with Atahualpa theme

I recently needed a solution to insert an Adsense add after the first post in WordPress. As you might have figured it out, Review Horizon is built around WordPress plus Atahualpa theme (currently at version 3.5.3). Inserting an Adsense add between posts is easier than you might think, considering WordPress design. On the first page, all the posts are displaying in a loop, and in this loop, you can add your own code as well. First, you will need to go to your Adsense account and create the Ad that fits your layout. For me, this was 728×90. Then, you navigate to Appearance->Editor and open the main index template (index.php). In this file you will need to make two changes:

1. Find the following text:

and before it, add:

The resulting code should look like this:

What the code you just added does is to initialize the variable $adsense_post_count to 1. The first time we’ll go through the loop we’ll make sure to increment its value so its value is 1 only the first time you go through the loop (for the first post only).

2. The next step is to actually test for $adsense_post_count value and if it is 1 (first post) to embed the Adsense code. In order to do this, find the following code in the index.php file.

Right after the above text, you need to include the following code:

Replace the text “Paste here your Adsense code” with the actual Adsense code you generated. The final version should look like this: (The green rectangle marks the Adsense code)

Don’t forget to click “Update File” button after you finished making changes. If you added everything the right way, you should see an Adsense add below the first post, just as I have on this website.

Note: You can play with the values and tests to embed the Adsense code after the second post, third or after each and every post (remember that you are only allowed to show up to three Adsense blocks). The process described can be applied to any other WordPress blog with any theme.

Related posts:

Tags: , , ,

  • http://anediblemosaic.com Faith

    This was so helpful! Thanks so much for taking the time to write out all the details like this. Worked like a charm. :)