Rae Hoffman

Thesis Tutorial - Advanced Ad Targeting

by Rae Hoffman on December 29, 2008 | Blogging It

If you're new here, you may want to subscribe to my feed or subscribe to me on Twitter, which is updated on a more frequent - and more meaningless - basis.

Since my last tutorial explaining the basics of the newly unveiled Thesis hooks was so well received, I thought I’d show you a bit more of how advanced the Thesis theme really is.

Not only will I “show” you, but I’ll also teach you how to take advantage of it on your own personal or professional blog.

If you look at my site, you’ll notice I run a large graphical ad above the fold on the Sugarrae website:


Ok, big deal Rae… we already know Thesis can use more custom ad targeting than most themes and that you can specify specific ads on a page by page basis:

Imagine you own an SEO blog but get a lot of traffic from people searching for routers due to a one off post you did… with Thesis, you can change your advertising on that single page to be router related without forcing you to rely on webmaster welfare to do it for you. - Source

And it is one of my favorite features about Thesis. You didn’t need to pick one “sitewide ad”. But after implementing “post specific” ads heavily, I needed to change a “non converting” ad. The problem was that I had to change the ad on every individual post (and good luck on remembering exactly which pages have which ads if you have several hundred posts).

Additionally, there was no way to specify an ad on a specific category page. It always showed the “sitewide” ad by default. While my blogging category page would do well showing an ad for the Thesis theme, my affiliate marketing category page would likely do better showing an ad for Linkshare or Pepperjam.

What I wanted was the ability to:

  • Specify a specific ad for each individual category page on my blog
  • Have posts filed in each category show the “category ad” for its category by default
  • Still be able to override the default ad on a single post if I choose

For example:

And by using hooks, I’m able to do all of the above. And now you can too.

Example: Specifying Custom Category Ads

Let’s say you want to show an ad for Acme Lightbulbs on the main page of your lightbulbs category and have that ad be the default ad for all single posts in the lightbulbs category. You also want to show an ad for Granny Apples in your apples category and have that ad be the default ad for single posts in the apples category. On all other site pages, you want to show an ad for Thesis.

Step 1: Upload the graphics you want to add to show up in your multimedia box to the custom images folder (thesis > custom > images > put your image here)

Step 2: Open your custom_functions.php file (thesis > custom > custom_functions.php) in your favorite editor

Step 3: Underneath the example code already in the file, place the following code (this code assumes your blog is in the root):

/* Multimedia Box */
function custom_ad_box() {
$categories = thesis_get_categories();

if ($categories) {
if (is_category('lightbulbs') || in_category($categories['lightbulbs']))
lightbulb_ad();
elseif (is_category('apples') || in_category($categories['apples']))
apples_ad();
else
generic_ad();
}
}

function thesis_get_categories() {
$raw_categories = &get_categories('type=post');

if ($raw_categories) {
$categories = array();

foreach ($raw_categories as $category)
$categories[$category - >slug] = $category - >cat_ID;

return $categories;
}
}

add_action('thesis_hook_multimedia_box', 'custom_ad_box');

The /* Stuff Inside Here */ is a label for the code below so you know what it is at a single glance. This is not part of the actual “code”. The word “function” tells Thesis you want it to do something. The custom_ad_box is what I decided to name that function.

The code underneath it basically says if that category is lightbulbs (the name you enter here is based on the “slug” of your category. So if your category url is www.domain.com/category-here/ then you would enter “category-here” without the quotes in place of “lightbulbs”) then the category should show the ad named “ad_lightbulb” (we’ll define how to create your ad next). - “if is_category”

If the category is not lightbulbs, then it will check to see if the category is apples and if it is, it will show the ad named “ad_apples”. - “elseif is_category”

If the category is not any of those defined in the code (in this case, lightbulbs and apples) then it will show the ad named “generic_ad”. - “else”

The second function in the above code, which is function thesis_get_categories is generic and should not be changed. The only changes you need to make are to the category names and names of the ads to be shown or adding additional “elseif is_category” lines.

The add_action('thesis_hook_multimedia_box', 'custom_ad_box'); line tells Thesis that you want it to add a function in the multimedia box and the function you want to add is the one we just created - the custom_ad_box function.

Now that Thesis knows what ad name to show on what categories, we need to define the ads themselves.

/* Custom Ads */
function lightbulbs_ad() {
?>
<a href="http://www.LINKLIGHTBULBADISLINKEDTOHERE.com/"><img src="THE-URL-OF-YOUR-LIGHTBULB-AD-IMAGE-GOES-HERE"></a>
<?php
}

function apples_ad() {
?>
<a href="http://www.LINKAPPLESADISLINKEDTOHERE.com/"><img src="THE-URL-OF-YOUR-APPLES-AD-IMAGE-GOES-HERE"></a>
<?php
}

function generic_ad() {
?>
<a href="http://www.LINKGENERICADISLINKEDTOHERE.com/"><img src="THE-URL-OF-YOUR-GENERIC-AD-IMAGE-GOES-HERE"></a>
<?php
}

As with before, the /* Stuff Inside Here */ is a label for the code below so you know what it is at a single glance. This is not part of the actual “code”. The word “function” tells Thesis you want it to do something. In this case, we’re defining three “functions” which are ads to be used in the custom_ad_box function we already defined above we decided to name lightbulbs_ad, apples_ad and generic_ad. Just put the HTML for whatever you want to appear in that ad slot there:

function name_of_ad() {
?>
HTML HERE
<?php
}

I first defined what ads should appear where and then defined the ads themselves underneath. However, if you find it easier, you can always define the ads first and then define where they should appear second. The order doesn’t matter as long as you both define the ads and in what categories they should appear.

That said, please note that if you were previously using the “custom code” option in the Thesis options panel, and there is code in there currently, you will need to remove the code and hit the big ass save button to see your new ad hooks display.

You can override the default ad shown for any category on a single post basis the same way as before:

I’m able to override the base choice I’ve made of what to show in that area for the majority of my site pages/posts in the Thesis control panel, all without ever having to touch a drop of code in the Wordpress templates by utilizing the custom key option on the specific page or post.

If you view the photo above, you’ll see I was able to select the “custom” key I defined in the Thesis control panel and “override it” and insert specific code to replace the normal “custom” key, only for this post.

If you’re still not using Thesis, get on board. This is the first of many cool tutorials I’ll have coming in the next few months that show why Thesis is quickly moving from being the “best theme choice” for a professional blog to being the “only choice”.

Subscribe to the Sugarrae feed | Follow Sugarrae on Twitter

Related Posts

Sugarrae runs on the Thesis WordPress Theme

Thesis WordPress theme

If you’re a blogger looking for a professional presence but don’t understand a lot of PHP, Thesis will give a ton of functionality that you wouldn’t be able to obtain otherwise with a simple control panel. With the incredible customization possibilities via Thesis hooks, you can easily make your blog stand out from the crowd.

Thesis allows bloggers to make more professional customizations than they may have ever deemed possible due to lack of coding knowledge. The theme allows me to run Sugarrae more professionally, with a focus on monetization that is more targeted than I’ve ever been able to achieve with a theme before. You can find out more about Thesis below:

{ 4 trackbacks }

A Few Good Posts
December 31, 2008 at 12:30 am
Thesis Theme for WordPress — Bill Bolmeier
January 16, 2009 at 12:13 am
Got questions about affiliate marketing? Ask Rae Hoffman | SEO Honolulu
February 4, 2009 at 2:05 pm
Thesis Tutorial - Customizing the 404 Page - Sugarrae
February 8, 2009 at 5:10 pm

{ 31 comments… read them below or add one }

1 Bruce Keener December 29, 2008 at 12:19 pm

Mighty fine stuff, SugarRae … thanks for sharing!

2 Wesley December 29, 2008 at 12:30 pm

Excellent tutorial, but for anyone who doesn’t use thesis, consider using the plugin “Widget Context“. It allows you to specify where exactly the widget should show; e.g. homepage only, categories only, specific URLs, etc etc..

3 Zack at Katz Web Design December 29, 2008 at 12:40 pm

You can go further and have multiple ads for each group, one of which is randomly chosen:

function apples_ad() {
$apples = array(’this HTML’, ‘that HTML’, ‘other HTML’);
$size = sizeof($apples) - 1;
$rand = rand(0,$size);
echo $apples[$rand];
}

That way, each advertiser can have multiple ad variations and do performance tracking.

4 Paul McIntosh - pmctosh December 29, 2008 at 12:55 pm

Great one Rae, well worth the wait.

Thesis really does rock. Still, thanks for making it even easier.

Rae Hoffman 5 Rae Hoffman December 29, 2008 at 1:06 pm

@Zack stick around… further tutorials coming ;-)

6 Erika December 29, 2008 at 1:58 pm

This was quite a nice surprise in my feed reader!

I generally build all of my templates from scratch, but I did purchase Thesis so that I can take a look at the backend and test it on my own. There’s a lot of good stuff in there, and if I didn’t have such a massive developer’s ego, I might use it myself. :)

7 Dragos Roua December 29, 2008 at 3:25 pm

Nice post, thank you.

You can go further, by controlling almost everything that goes into the multimedia box, and by that I mean various content for each post, like signup forms, for instance. Not only advertising, that’s my point :-)

Rae Hoffman 8 Rae Hoffman December 29, 2008 at 3:35 pm

@Erika I have to ask… why wouldn’t you use Thesis just because you know PHP? If you’re using wordpress, then obv you’re not adverse to using software you didn’t build. I can’t get why one would want to customize a template page by page rather than being able to affect EVERYTHING on the theme via two files and be able to change layout at whim with no change of theme files added to that.

9 voordelig lenen December 29, 2008 at 4:54 pm

Thanx for this great piece of code. Will use this indeed for ad targeting, and besides ad targeting it is also a useful script for specific category design futures

10 Zack at Katz Web Design December 29, 2008 at 7:40 pm

I wrote an article about randomizing some attribute text, tags, and placement that does some of the same things. I’m going to be developing a plugin to this effect. For those interested in SEO, check out my post: Value-Based SEO Randomizer

11 kristarella December 29, 2008 at 8:10 pm

Nice one Rae! I like how you’ve used category slugs instead of IDs. That’s a downside of the native is_category() that you’ve overcome nicely.

12 Allyn December 30, 2008 at 1:59 pm

This may be a dumb question, but could I use “tags” instead of “category”?
thanks,
AL (almost sold on Thesis)

13 Vivek January 3, 2009 at 9:51 am

If you are using Adsense you may wanna tweak it little bit by adding 404 protection to stay clean with Adsense TOS:

/*
* Multimedia Box Ad 350×250
*/
function custom_ad_box_300×250() {
if ( ! is_404() ) {
echo ‘
// your code
‘;
}
}

add_action(’thesis_hook_multimedia_box’, ‘custom_ad_box_300×250′);

14 Loretta January 3, 2009 at 7:57 pm

I didn’t realize that Thesis did so much until you started these tutorials. Now I’m seriously contemplating ordering and I’ve never *bought* a wordpress theme before. But, all the work is done already, why bother messing with code when I don’t have to.

15 JohnnyPea January 8, 2009 at 5:00 am

Thank You for this great tutoria…but it seems to be not working for me (I am new to coding and Thesis too), it shows this error : “Parse error: syntax error, unexpected ‘….on line 59″ and the line is: “$categories[$category - >slug] = $category - >cat_ID;”

Another thing please is theren anyway to modify this to show particular picture on “is_archive() ” and normal rotating pictures on everything else? Thank You in advance. Have a nice day.

J.

Rae Hoffman 16 Rae Hoffman January 8, 2009 at 1:27 pm

@JohnnyPea you need to make sure that none of the quotes, dashes, < or > signs have any messed up encoding in them.

17 JohnnyPea January 8, 2009 at 3:55 pm

Thank you for an advice. I would like to ask one more thing, how can I make images display exactly in mediabox area(automaticaly resize)?- how it does for default image rotator or image custom tag. Thank you again.

18 daveraffaele January 9, 2009 at 9:54 pm

As a thesis user I couldn’t be more impressed with how well your site is designed. You have taken the same theme I started with and created something extremely thorough, clean, and functional.

Oh yeah…and the content rocks too. :)

thanks for sharing what you know.

dave

19 darla Sycamore January 10, 2009 at 2:07 am

I am not a web designer this stuff is way too advanced for me. I got thesis for SEO and analytics and rotating media box.

Where can I go to learn some basic skills in the context of thesis

Rae Hoffman 20 Rae Hoffman January 10, 2009 at 9:18 am

@darla I’ve written a Thesis Hooks for Dummies post that might help you.

21 Michelle Lamar January 10, 2009 at 1:12 pm

I love your blog and your EZ way of explaining things like hooks, wordpress and just the web in general. Thanks for explaining it to the rest of us! I’ve looked in the Thesis forum area, which is awesome, but I can’t find the plug in/hook/hack for how to get the “Related Posts” function you have at the end of your posts. If you could find it in your heart to tell me, I would be even more grateful to you than I am now. Thanks and keep it coming!

22 Axekap expertise January 12, 2009 at 10:28 am

Looking for a professional theme for a new wordpress , just found thesis and your article. Given the quality of design and the numerous ways
of customizing, think we are also going to use it … thanks to all these great explanations on hooks !

23 han January 14, 2009 at 2:24 am

Great post! One question though. How do you use the custom key option on the specific page or post? And if possible, can I ask how did you put different custom messages in each different categories? I really like how you put it. Thanks!

24 Deb January 22, 2009 at 6:08 am

Good looking theme, but much too geeky. All I see are instructions about hooks and code everywhere. I already know more PHP than I ever wanted. Whatever happened to drag and drop?

25 Chris Mitchell January 30, 2009 at 2:08 pm

Hi Rae - have you played with Thesis 1.4.1 yet? It seems to break your custom function code — I had it working perfectly on Thesis 1.3.3 but with the upgrade I got this error

Fatal error: Cannot redeclare thesis_get_categories() (previously declared in //wp-content/themes/thesis/lib/functions/content_functions.php:174) in //wp-content/themes/thesis/custom/custom_functions.php on line 48

Line 48 refers to the
return $categories;
}
}

part of the code.

If you have time to offer any insight, that would be great - and thankyou for the awesome Thesis tutorials.

Best
Chris

Rae Hoffman 26 Rae Hoffman January 30, 2009 at 8:42 pm

Hey Chris,

Just remove this part ONLY:

function thesis_get_categories() {
$raw_categories = &get_categories(’type=post’);

if ($raw_categories) {
$categories = array();

foreach ($raw_categories as $category)
$categories[$category - >slug] = $category - >cat_ID;

return $categories;
}
}

From the code YOU put into your custom functions file. Pearson built this function into Thesis 1.4, so the error your getting is simply complaining that your defining something in your code that is NOW, as of 1.4, predefined. :)

27 Chris Mitchell January 31, 2009 at 12:13 am

works like a charm Rae - thanks!

28 John H. Farr February 1, 2009 at 3:55 am

Excellent! I see how this targeting works for categories. Can I do the same thing with tags, though?

I’d like a specific image to appear in the multimedia box for all posts of a certain tag, in other words, so could I just substitute “tag” and “tags” for “category” and “categories” in the above examples?

Please say yes. :-)

29 john February 1, 2009 at 12:04 pm

I’m wondering if it is preferable to use a Widget or a Hook to display an adsense sidebar ad that requires some PHP to generate a custom channel ID. I have it working in a widget utilizing the widget that allows PHP processing, so it is working but I’m just wondering if doing this in a widget is the best approach and whether I would be better off doing it in a hook.

Thanks

Rae Hoffman 30 Rae Hoffman February 2, 2009 at 9:50 am

Personally, lately, if my sidebar requires PHP, I do it via a hook.

31 sashahawke April 9, 2009 at 11:52 am

Thanks Rae!

I’ll definitely look into Thesis, it seems like a really good theme. I really like the idea of dynamic adsense sidebars.

Leave a Comment

Want to add a picture and social profile links to your comments here on Sugarrae.com? All you gotta do is be be logged in when commenting after filling out your user profile.

Not registered at Sugarrae? Register now!

Please note that by clicking submit, you agree to abide by the comment policy.