Friday, 15 January 2010

How to show AdSense Ads After First or Every Post in Wordpress Blogs

This is the most searched and frequently asked question that How can add google ads at home page after posts.


To do this, You need to edit famous wordpress loop. Such a loop can be found in your current wordpress theme’s index.php. There may be other files present in your theme directory which uses loop like archives.php. They may need to be updated as well.


Listed below are steps to edit loop in index.php file. Similar steps can be performed for other files too, if required.


1. Go to Wordpress Dashboard >>Design >> Theme Editor.


2. Click on index.php or Main Index Template file. It will open in Theme Editor on left.


3. Find a line:


<?php while (have_posts()) : the_post(); ?>

4. Add before it:


 <?php $adcount = 1; ?>

5. So final code will look:


<?php $adcount = 1; ?>

<?php while (have_posts()) : the_post(); ?>

6. Now find:


<?php endwhile; ?>

7. Add before it:


<?php $adcount++; ?>

8. So final code will look:


<?php $adcount++; ?>

<?php endwhile; ?>

9. Now inside loop, i.e. after “while” line but before “endwhile” line put your AdSense codes. Exact location depend on your choice between ads before post, ads after post, etc. Important thing is you have to put your AdSense code in a if-block. below is an example…


<?php if ($adcount == 1) : ?>

<!-- Put Your AdSense Code here -->

<?php endif; $adcount++; ?>

That’s it. Save changes and check if it works.


Variations…


1. If you want to put ads after first two post…


In step 9 code, Change $adcount == 1 to $adcount == 2


2. If you want to put ads after first three post…


In step 9 code, Change $adcount == 1 to $adcount == 3


3. If you want to put ads after each post…


In step 9 code, Change $adcount == 1 to $adcount == 3. Yes this is same like above. Because AdSense only allows maximum three ad-units on a a single page. This way you avoid blank space created after each post when you directly put AdSense code after each post.


If any problem, just contact me at jamshed_206@yahoo.com

8 comments:

  1. You are now listed on FAQPAL24 March 2010 at 09:10

    Show AdSense Ads After First or Every Post in Wordpress Blogs...

    With the help of this article you can show google ads after every post in wordpress without any plugin....

    ReplyDelete
  2. I have to say, every time I come to adsenseurdu.com there is another fascinating post up. One of my friends was talking to me about this topic several weeks ago. I think I will e-mail them the link here and see what they say.

    ReplyDelete
  3. sunglasses shop4 June 2010 at 07:44

    thanks for great informations It's a wonderful

    ReplyDelete
  4. Cheap Insurance Quotes16 June 2010 at 12:39

    I really enjoyed this post, especially the “examples in this post” portion which made it really easy for me to SEE what you were talking about without

    even having to leave the article. Thanks

    ReplyDelete
  5. I like your blog very much!

    ReplyDelete
  6. I cant see to make it work, it just displays it every 1 posts

    ReplyDelete
  7. i cant make it work. on every 2 or  3 posts it keeps showing every 1 posts. can you email me on how to do it on every 2 posts,

    ReplyDelete