How To Add Category and Tag Based Advertising and Content to Your Blog

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.

Bookmark and share this post:
  • del.icio.us
  • Facebook
  • LinkedIn
  • MySpace
  • Digg
  • StumbleUpon
  • Sphinn
  • Ping.fm
  • FriendFeed
  • Reddit
  • Identi.ca
  • Posterous
  • Tumblr
  • email
  • RSS
Here's more cool posts similar to this one:
  1. How To Get Google AdSense and Display Ads on Your WordPress Blog
  2. How to show number of comments in WordPress
  3. How To Manage Ads and Track Clickthroughs with AdRotate Plugin in Your WordPress Blog
Topic: Blog Advertising
Tags: Advertising, Customize, How-To, Make Money Online, php, WordPress
| Antti Kokkonen | Leave a comment

Comment policy: We're gonna be like little Fonzies here. And what's Fonzie like? Cool. Correctamundo, and that's how we roll here -- cool. Critical is OK, but if you're rude, spam or otherwise misuse the blog comments, I will delete your comment. Do not put your URL in the comment text. Use your PERSONAL name (yourname@example is cool, example.com without your name is not). Have fun, be excellent to each other and thanks for adding to the conversation!

Leave a comment

Comment policy: We're gonna be like little Fonzies here. And what's Fonzie like? Cool. Correctamundo, and that's how we roll here -- cool. Critical is OK, but if you're rude, spam or otherwise misuse the blog comments, I will delete your comment. Do not put your URL in the comment text. Use your PERSONAL name (yourname@example is cool, example.com without your name is not). Have fun, be excellent to each other and thanks for adding to the conversation!

Enter your name, email and website. Write your comment and click submit. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

blog comments powered by Disqus