Welcome to AdamBarber.tv

To request a quote, please call Adam at (856) 438-0002

Inserting Adsense on Category Pages

by adam on July 1, 2009

As you can see on this site, I am using Thesis hooks to insert adsense boxes both above, and below the content on single post and category pages.

Adsense above select category headers.

Adsense above select category headers.

In addition to being able to quickly inject the boxes, I wanted to make sure I could exclude the adsense from particular categories, like anything Thesis related, for example. There are a couple steps to do this.

Step 1 – Add the following to custom_functions.php

/* Custom Adsense Insertion */
function custom_adsense_insetion(){
 if (is_single() && in_category('thesis')|| is_category('thesis') || is_front_page() )
{ }
else { ?>
<div class="adsense">
#### YOUR ADSENSE CODE HERE ####
</div>
<?php
}}

So we have a couple of things to look at. In order to select which categories we want ads on, I’ve used an if statement. In my particular case, I knew I had only a few instances where I didn’t want the ads automatically inserted, so I decided to go with a negative-case setup. Within the “if” brackets, I define which pages I want the function to display nothing on. The fist is single post pages for anything in the Thesis category. The second is the Thesis category listing its self. The Last is the home page. Each case is separated by “||” which denotes “or.”

Any case in the else area will automatically get the adsense. Depending on how many pages you are applying the code to, you may want to reverse this.

Step 2 – Add actions! I decided to simply use the before and after content hooks. It isn’t 100% what I was going for, but it gets the job done for right now. You can experiment with which particular thesis hooks work best for your setup.

Copy and paste the following into custom_functions.php

add_action('thesis_hook_before_content', 'custom_adsense_insetion');
add_action('thesis_hook_after_content', 'custom_adsense_insetion');

Step 3 - The last thing we need to do is style the div to ensure the positioning is correct. I just added a margin to the top and bottom, and an “auto margin” to the sides to ensure it stays centered. The style code goes in the custom style sheet.

.custom .adsense { margin: .3em auto; text-align: center}

That’s it! If you have any problems getting anything to work, leave a comment, or follow me on twitter at http://twitter.com/adambarber. And please let me know if you would like to see any particular tutorials!

Popularity: 16% [?]

{ 1 trackback }

47 Customizations for the Thesis Theme
April 11, 2010 at 12:25 pm

{ 4 comments… read them below or add one }

Wordpress tutorial August 3, 2009 at 2:31 am

nice post for integrate adsense on thesis, i always search this tutorial, and get it in your post

Quixotic Media January 4, 2010 at 10:44 pm

Loving the site man. Smooth design.

Admiral February 26, 2010 at 7:39 pm

Thanks for the custom_fuction command! Been looking everywhere for something like this.

By the way, when using the code you provided, how can I exclude certain pages, like if I don’t want to include the following pages:

About Us
Privacy Policy
Contact Us

What can I add to exclude the code from certain pages?

Looking forward to hearing from you

tangologix June 9, 2010 at 11:19 am

nice written . well informative article. i saw it operation on theappsplanet site too . nice written info man. thanks for posting.

Leave a Comment

Previous post:

Next post:

Design by Adam Barber, Thesis by Chris Pearson.