Post image for How To Add Adsense To Every Post On Your Thesis Theme WordPress Blog

How To Add Adsense To Every Post On Your Thesis Theme WordPress Blog

by Mike Steben on September 22, 2010

If you’re reading this post then you already realize that Thesis is quite possibly the best thing since, well, WordPress itself. I had wanted to be able to add my Adsense code to Thesis in such a way that my ads would display on every post, align left, with text wrapped around the ad. After a great deal of research and lots of tweaking I had finally found a Thesis hook to make this work. Here’s how you can modify your custom_functions.php file to add your Adsense code to every post on your blog.

First of all, I’m not a “coder” so I wanted to thank Adam over at adam.tv for posting the code I used as a “framework” for my Adsense hook. Secondly, please be sure to backup your custom_functions.php file before modifying it! Here is the code that I added to my custom_functions.php file so I can display Adsense ads on all of the posts pages in my blog;

/* Custom Adsense Insertion */
function custom_adsense_insertion(){
if ( is_front_page() || is_page(about) || is_page(contact-form) || is_page(privacy-policy) )
{ }
else { ?>
<div class=”adsense” style=”margin: 10px; float: left;”>
!!!!!!!! Your Adsense Code Goes Here !!!!!!!!
</div>
<?php
}}

add_action(‘thesis_hook_before_post’, ‘custom_adsense_insertion’);

Here’s a color coded explanation of which portions of this Thesis hook that you need to modify in order to change your Adsense placement.

Red - This line is used to prevent Adsense ads from appearing on certain pages that you specify. I for example didn’t want Adsense ads appearing right smack in the middle of my privacy policy as well as on some of my other static pages.

Green - This line specifies that all of my Adsense ads will be aligned to the left with text wrapped around them. You can of course modify this line to fit your needs.

Brown - This line “tells” Thesis to display my Adsense ad before every post i.e. under the post title and post image.

Final thoughts…

Remember that this code will add your Adsense ads to EVERY post page on your blog. You will need to ad your Adsense code to other parts of your site such as the header, widgets, footer, etc… using a different method. This method of placing Adsense ads using Thesis hooks worked for me, hopefully it will work for you too!

No related posts.

Written by | Mike Steben

Mike enjoys computers and technology, playing the piano, selling stuff on eBay, playing video games, cooking, and eating at Teddy's Pizza. He started CompuNoodle in 2009 and loves to hear from his readers.

{ 9 comments… read them below or add one }

Jay Castillo November 10, 2011 at 9:04 pm

Finally, a code that actually works! Actually there are a lot of articles out there for the same purpose but yours is the first where margins around ads (so they don’t touch the text in the content) actually work! I deployed this in my blog although I modified it a bit to fit my needs.

Thanks Mike!

anuj@SEOTIPS October 11, 2011 at 3:39 am

Thanks so much for this. I’m not even using ads, just custom HTML & PHP. But this is the only place I’ve found that articulates how to use the $post_count attribute to target specific posts’ hooks. A total lifesaver (or at least hairsaver, as I was pulling it out when the documentation said the attribute could be used but didn’t say *how*). Bookmarked!

Nikko April 11, 2011 at 10:43 am

doesnt work for me either, copied it and nothing happens.

Mike Steben February 23, 2011 at 9:52 am

Good deal! I’ve become quite fond of this little timesaver as well! Thanks for visiting CompuNoodle!

sharell February 22, 2011 at 5:25 am

Great, it works for me. Thanks so much! Very useful indeed. :-)

Mike Steben January 30, 2011 at 12:03 pm

Good morning Nicole, I’ve now triple checked my code and… I found a discrepancy between my actual code and what I had posted in this article. Somehow, I had neglected to paste in the class attribute “adsense” which should have been specified as part of the div tag. The accuracy of the information posted on CompuNoodle is very important to me so I would much rather admit to a mistake as opposed to making excuses. Thanks to you an Richard for pointing this out, I hadn’t received any comments on this post thus far and had therefore assumed that all was well and good. Ctrl + V can be a dangerous thing, even in the hands of a 10+ year computer professional!

Nicole January 30, 2011 at 11:18 am

Richard – did you ever find a solution for this? I have the same problem….

Mike Steben January 14, 2011 at 9:51 pm

Sorry to hear this, I actually use this on all of my sites and it works flawlessly. The one thing that comes to mind immediately is to make sure that you aren’t running an ad blocker, or if you are you need to make sure that you exempt your own domain. I did that by mistake when I first built this site… Oops ;) The other thing is to make sure that you give newly created Adsense ads up to 10 minutes to appear on your page. You also need to remember that you can only have a maximum of 3 ad units and 3 link units on each page. Have you made any other modifications to your custom functions file?

Richard January 14, 2011 at 6:57 pm

This doesn’t work for me. I’ve copied exactly into my custom_functions.php and the post pages just come up blank. I’m running on Thesis 1.8. Any ideas?

Leave a Comment

Previous post:

Next post: