Want to display certain ads only on certain categories or tags? This quick guide will show you how to do that, for example show banner ads based on category.
If you have blog with different categories, you might consider customizing the advertising shown on posts in spesific category or categories or posts with certain tags. And it doesn't have to be advertising, you can show different links on different categories and tags.
For example you can:
- Show links to different themes and plugins on your "WordPress" tagged posts, and
- Amazon.com Associates cookbook links on your "Cooking" category.
- Or just display your best posts in the "Fishing" category.
- etc.
The possibilites are endless, so you can do whatever you can show anything you want and need to show, based on the tags or categories of the posts.
I wanted to do this, because many of the readers who come in and check some of my World of Warcraft material, or EVE Online posts, are not necessary interested in the WordPress content. And if I write about advertising on a blog and how to utilize a plugin to do it, that gamer just wanting to know how to powerlevel mining on the new alt character, most likely does not want info on latest WordPress Premium Themes or such. So I wanted to customize the advertising, extra content and links I show on different posts.
First, I utilized the "Yet Another Related Posts Plugin" to show the related links, so that was easy and does superb job at providing the related post links.
But getting spesific advertising in those posts wasn't hard either..
If you have the same kind of situation, and want to display advertising or special links based on the categories or tags of your posts, I'll show you a quick way to achieve that. You do have to dig into your WordPress theme php-files, or find a plugin to insert the code, but it's really simple...
Category Based Advertising and Content
Example from WordPress Support forums post "Show banners/ads based on Category?":
<?php if ( is_category( 'cat') ): ?>
<!-- Insert code for cat ad here -->
Test Cat
<?php elseif ( is_category( 'dog') ): ?>
<!-- Insert code for cat ad here -->
Test Dog
<?php else: ?>
<!-- Insert code for generic ad here -->
Test Generic
<?php endif; ?>
Add this kind of code before or after the post in the single.php and edit it as you like (I didn't test it on other pages yet, but let me know if you have something like this running already):
<!-- Category based content START -->
<p>
<!-- Use Category ID -->
<?php if ( in_category('1') ): ?>
<!-- This code will be displayed if the post is in category with ID 1 -->
<!-- Category ID 1 code starts here -->
<!-- INSERT advertisement code for Category 1 here -->
<!-- Category ID 1 code ends here -->
<!-- Use Category Name -->
<?php elseif ( in_category('Gaming') ): ?>
<!-- This code will be displayed if the post is in category named 'Gaming' -->
<!-- Category 'Gaming' code starts here -->
<!-- INSERT advertisement code for 'Gaming' here -->
<!-- Category 'Gaming' code ends here -->
<!-- Use ID, Name or Category slug together -->
<?php elseif ( in_category(array(2, 'Blogging', 'personal-development')) ): ?>
<!-- This code will be displayed if the post is in any of the categories defined in the array -->
<!-- Category ID 2, 'Blogging' or 'personal-development' code starts here -->
<!-- INSERT ad code for several categories here -->
<!-- Category ID 2, 'Blogging' or 'personal-development' code ends here -->
<?php else: ?>
<!-- This code will be displayed if the conditions above don't hit (you can also remove the else) -->
<!-- Code for all other categories code starts here -->
<!-- INSERT code for generic advertising or other content here -->
<!-- Code for all other categories code ends here -->
<?php endif; ?>
</p>
Remove the unnecessary lines and comments if you like and you're all set. You can use category IDs or category names, and even category slugs work. For detailed info on how the "in_category()" works, check the WordPress Codex: Conditional Tags -page.
Tag Based Advertising and Content
Exactly like with categories, you can customize the content based on the tags of the posts:
<!-- Tag based content START -->
<p>
<?php if ( has_tag('WordPress') ): ?>
<!-- This code will be displayed if the post has a tag named 'WordPress' -->
<!-- Tag 'WordPress' code starts here -->
<!-- INSERT advertising code for posts with tag 'WordPress' here -->
<!-- Tag 'WordPress' code ends here -->
<?php elseif ( has_tag(array('How-To', 'Monetize')) ): ?>
<!-- This code will be displayed if the post has at least one tag defined in the array -->
<!-- Tag 'How-To' or 'Monetize' code starts here -->
<!-- INSERT advertising for several tags here -->
<!-- Tag 'How-To' or 'Monetize' code ends here -->
<?php else: ?>
<!-- This code will be displayed if the conditions above don't hit (you can also remove the else) -->
<!-- Code for all other tags code starts here -->
<!-- INSERT generic advertising code or other content here -->
<!-- Code for all other tags code ends here -->
<?php endif; ?>
</p>
Detailed Info and Afterword
I wanted to get this done quick on my blog, so I only added and tested this in the single posts and placed the code to show the content after the posts.
So if you try it in the sidebar, header or footer, let me know how you do with it. Also if you know any great plugins that do similar, please share the experiences and leave a comment.
Here's more cool posts similar to this one: