Tag Archives: Wordpress Programming

Display Ads Only On Selected WordPress Posts

Every blog is a little different and has it’s own unique requirements. On some blogs we may want to place an ad on every post, on others we may not. A good example is this blog. There are a few posts that I make where I feel an ad is useful and lends itself to have a relevant contextual ad placed on the post.

There is a very simple way to handle this issue.

Yes, there are plugins available that handle all kinds of issues with ads, however, I just wanted an easy way of dealing with it, so here is what I came up with.

Two lines of code and using Custom Fields does the trick,

First I wrap my ads on the single page with this code.

<?php
if (get_post_meta($post->ID, ‘ads’, true)) {
?>

AD CODE HERE

<?php
}
?>

This will cause ads to only show when there is a custom field named “ads” with a value of anything but 0. And once you have inserted the custom field you never have to add it again, just apply a value, and since this is a simple boolean expression, any value except 0 will cause the ads to display.

Examples:

No Ad : Weekly CyberCoder Tweets for 2008-11-30

With Ad : Online Residual Income

You could go the other way and have the default show the ad, or include a couple statements to change the ad location, color or anything else you could think of. This is just a simple solution for a specific need and I hope it helps someone.

If you want more options using basically the same technique I would highly recommend this tutorial, How To Automatically Disable Ads On Selected Posts In WordPress.

WordPress Love and Hate

I am a big WordPress fan, of course some of that comes from me liking to do PHP coding and WordPress is a very open platform to work with leaving lots of room for coding creativity.

But if you watch this WordPress Twitter Search it seems every so often someone has a little spat with their WordPress installation. Like WordPress is picking on them personally or something, lol.