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.
In my last WordPress tutorial, I discussed a custom-fields based ad revenue sharing system. 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, I’m talking of in-post ads here (not those in the sidebar or anywhere else). There’s 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 don’t need to define a custom field every time you write a post. You just need to do it on the post you don’t want the ads in. If the custom field is not there, the code will show the ad.
So, for a post you don’t 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 won’t 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 doesn’t show the ad. If it’s not, from that particular category, it goes ahead to show the ad.
And that’s all you need to do. The advantage of this solution is that even older posts under the same category get affected, and you don’t 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. That’s easy. Use this code:
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 doesn’t 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 isn’t 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!
Some Books You Might Like
If you’re interested in WordPress (I’m sure you are, if you’re reading this post!
), here are some books on the subject. These books are tutorials on the WordPress platform, and teach you various things: from setting up WordPress as your blog, to marketing it. Take it from me, these books are highly recommended (Note: These links are affiliate links.) :
- WordPress for Business Bloggers: Promote and grow your WordPress blog with advanced plug-ins, analytics, advertising, and SEO
- WordPress Entrepreneur: How to Setup, Customize & Use a WordPress Website
- WordPress Complete: A comprehensive, step-by-step guide on how to set up, customize, and market your blog using WordPress
What do you think of this tutorial? Was it helpful? Do you have a better way of accomplishing the same task? We’d love to know!
If you liked this post, consider subscribing to the RSS feed (what's RSS?) to get updates on new blog posts.







Excellent technique for displaying ads on selective posts. I used almost similar technique on a company blog for displaying ads on 3 old posts.
nice one, I was in search of it from last month. You solved my problem… thanks siyab.
Nice concept, I wonder if we can take it a step further: perhaps use a plugin to store PHP/HTML/JS snippets, allow defining the location to place the snippet relative to post, and then the custom field that overrides to disable?
I think it would be killer.
Sid, thanks for dropping by… I think that is possible…. and yes, it would be killer!
hi, thanks for the info. i was having problem trying to think of how to show ads on certain category on my mini site as i know nothing about html or php coding.. so i search thru google and found your site. thanks for the information…
Many languages use the systems redundantly, for instance German, with its rich gender and case systems, moderate use of agreement, and fairly strong constraints on phrase order. ,
This is acceptable in terms of search engine optimization. Nothing looks to rag upon them compared to that.Amusingly enough, this is exactly was talked about ten years ago at the last big blackhat about google in 1993.