How To Automatically Disable Ads On Selected Posts In WordPress

WordPress is such a wonderful blogging platform to work with. The possibilities are endless. In this post, I discuss using custom fields (or otherwise) to display ads on only selective posts.

Whether you have a single authored blog or not, you do sometimes may wish to not display ads in particular posts (such as press releases, or announcement posts). Mind you, Im talking of in-post ads here (not those in the sidebar or anywhere else). Theres a simple way to do that, using a few lines of code. Read on!

Step 1: The Code

First, place this code where your ads normally go (above and/or below the post):

<?php $ad_display = get_post_meta($post->ID, "ads", true);
if ($ad_display == "no"){ ?>
<! NO ADS SHOWN >
<?php } else { ?>
<! SUBSTITUTE AD CODE HERE >
<?php } ?>

Substitute the <!-- SUBSTITUTE AD CODE HERE --> part with your ad code.

Now to explain: First, what the code does is that it gets the value of the custom field with the key ads (this you define in the post). It assigns this value to a variable ($ad_display). If the value is equal to no, the code will not display the ad. If the value of this variable is not equal to no, the code will go ahead to display the ad. Simple enough?

Step 2: Define the custom field

This code works in such a way that you dont need to define a custom field every time you write a post. You just need to do it on the post you dont want the ads in. If the custom field is not there, the code will show the ad.

So, for a post you dont want ads on, just add a custom field with the key as ads, and value no

So, any post that has this particular key/value combination does not contain the ads. But, a disadvantage of this solution is that it wont be applicable to previous posts of a similar nature (you will have to manually edit all of them). Perhaps another solution can help?

Identifying By Category

Suppose you have categorized all such posts under a particular category (like news, for example). You know all the posts in this category should not contain ads. So instead of using custom fields on every post in this category, why not check whether a particular post is from this category, and display ads accordingly?

Use this code:

<?php if (in_category(category-id)) { ?>
<! NO ADS SHOWN >
<?php } else { ?>
<! SUBSTITUTE AD CODE HERE >
<?php } ?>

If you notice, this piece of code is similar to the above one, with just a few changes

And replace category-id with the ID of your particular category. Now what happens is that the code checks whether the current post is under the category with the ID that you specify here. If it is from that category, it doesnt show the ad. If its not, from that particular category, it goes ahead to show the ad.

And thats all you need to do. The advantage of this solution is that even older posts under the same category get affected, and you dont have to go about editing each one.

Using The Two Together

Suppose you want the best of both worlds. You want to use custom fields as well as category identification. Thats easy. Use this code:

<?php $ad_display = get_post_meta($post->ID, "ads", true);if ($ad_display == "no" || in_category(category-id)){ ?>
<!-%u2013 NO ADS SHOWN %u2013->
<?php } else { ?>
<!-%u2013 SUBSTITUTE AD CODE HERE %u2013->
<?php } ?>

Remember to substitute category-id with the ID of your particular category. Now, we put both the custom fields part and the category identification parts into one, using the || logical operator. What this does is that it checks if the current post has that particular key/value combination or if it is from that particular category (the category ID of which you supply) ? If any of these conditions return true, the code doesnt show the ad. If neither of these conditions is true, the code shows the ad. Simple enough?

The Sky Is The Limit

This technique that I just showed you isnt only applicable (or practical) for displaying ads. You can use it for so many other tasks, such as displaying information about a press release, and so on. The sky is the limit!

Leave a Reply