Web Hosting

Thesis Tips: How to Display Relevant Advertisements on Posts

Thesis Theme for WordPressDisplaying relevant ads alongside search results is what made Google rich.

So if you’re promoting a number of different products on your blog how can you make sure you’re displaying the most relevant ad to your readers?

It’s easy to take the shotgun approach and just display all your ads to all your readers all the time.

But what if there was a way to be more targeted and display ads that are more relevant to the page that’s being read?

Luckily there is – and with Thesis it’s quite easy to do.

On this site I’ve linked ads for different products to different categories. (You could also link ads to specific tags if you wanted a more finely focused approach).

So if someone is reading an article in the Internet Marketing category they’ll see an ad for Aweber Email Marketing.

And if they’re reading an article in the Blogging category they’ll see an ad for 31 Days to Build a Better Blog.

How to Use Thesis Hooks to Display Relevant Ads

So here’s how to use the Hooks function in Thesis to display ads that are relevant to the page being read.

Step 1: Upload the banner images you want to display to your server.

Step 2: Work out which banners you want to display for each of your categories

Step3: In order to position and style your ads you’ll need to create a div in your custom.css file and assign it some attributes. I called the div ‘adverts’ and simply specified that it should be center aligned:

margin-left: auto; margin-right: auto;

Step 4: You’ll need to create a conditional function – something that says if the article on the current page is in X category, then display Y banner. This is your conditional function statement:

{ if (is_single() && in_category('Your Chosen Category')) { ?> <div class="your_div"><a href="http://www.Your_Affiliate_Link.com"><img src="http://www.the_url_of_the_banner_for_your_product.jpg" alt="a suitable alt tag" width="468" height="60" /></a></div><?php}}

The line after the first squiggly bracket states that if the article is on a single page and in the specified category then do what has been specified inside the second squiggly bracket (which is to display your chosen banner ad).

There are two squiggly brackets at the end to close the first and second open squiggly brackets.

Step 5: Next you need to decide where you want these ads to be displaid. I display them above the post and, therefore, use this hook:


Using Thesis OpenHook

If you’re using Thesis OpenHook (which I wrote about here) it’s easy to work out where you want to place your ads because all the hooks are listed.

You can select a hook, paste the conditional function code above (updated with your own links!) into the box and view the site to see whether you like where it sits.

If you’re not happy simply cut and paste your code into a new hook and repeat the process until you’re happy with its position.

Step 6: Remember to click the ‘Execute PHP…’ check box, hit the Little Save button and you’re done.

Using the custom functions file

If you’re using the custom functions file you’ll need to define the code above as a function in your custom functions file.

First name a new function:

function unique_function_name()

After the function name add the conditional function statement above.

Finally add that function to a hook:

add_action('thesis_hook_before_headline', 'unique_function_name');

So putting all that together, here’s what the finished code snippet would look like in the custom functions file:

function single_post_wpthesis() {
     if (is_single() && in_category('Wordpress')) { ?>
<div class="advert"><a href="http://www.affiliate-link.com"><img src="http://www.image-url.jpg" alt="Suitable alt text" width="468" height="60" /></a><br /><br /></div>
add_action('thesis_hook_before_headline', 'single_post_wpthesis');

That takes care of one banner ad for one category.

If you want to display different banners for different categories then repeat the steps above but change the category, affiliate link and banner URL each time.

Be very careful to define your category exactly as it’s named on your site – if you get the capitalization wrong your ad won’t display.

If you’re using your custom functions file remember the health warning!


OK, that’s all there is to displaying ads that are relevant to the page that’s being read.

If you have any questions or need clarification leave a comment..!


Martin Malden

Web Hosting