There are many plugins which are useful when it comes to displaying ads to your visitors but there are only a few of them which allow you to determine to whom and when you should. Who Sees Ads does help you to determine to whom and when you should display advertisements, though, there is a limitation with the plugin as it does not allow you to control the ads shown on single post level.
In this post we will talk about the quick and easy way to hide advertisements for any particular post by making some minor changes to your theme.
The Condition To Skip Advertisements For Single Posts
Talking about conditions whatever you do there is always a condition under which you perform any action. We will use a similar logic and create conditions under which the advertisements should not be displayed for certain posts.
The condition we will be using is quite simple and will fulfill the following statement, ” if something happens do this otherwise do something else”.
This can be simplified into lay terms as “if you are hungry buy a burger” “else don’t buy a burger”. As you can see the conditions you apply are quite simple and pertain to you to doing some kind of activity under different conditions.
The Code To Skip Advertisements In Single Posts
To skip advertisements for single posts you will have to manually edit the file “single.php” of your current WordPress theme. I recommend you edit your theme through the WordPress Admin Panel since it will help you catch any errors and save you a lot of trouble.
Once you have opened the single.php navigate to the place where you have placed the advertisements and change the code within the WordPress Loop from;
…………
Your Advertisement Code
…………
to;
if(get_the_ID() != xx) {
…………
Your Advertisement Code
…………
}
Replace xx with the post id for the post which you do not want to show advertisements for. You can also skip multiple posts from displaying advertisements by adding the OR condition in the following way;
if(get_the_ID() != xx || get_the_ID() != xx) {
…………
Your Advertisement Code
…………
}
For those who are unaware of programming “||” represents OR in programming language and if you are curious AND is represented by “&&”.
How do you get the IDs of the posts you want to Skip Advertisements for?
To find the IDs for the posts you want to skip advertisements for, you will need to login to your WordPress administration panel and navigate to the Manage option. Once there find the post you want to skip the advertisement for and hover over it, you should see the ID of the post in your status bar. Check the screenshot below;
If you do not see the id in the status bar you can find out the ID by clicking on the post and going into edit mode where you will see the ID for the post in the URL.
Once you make the changes to your advertisements they will not display for the posts you chose to skip.
If you do have any questions feel free to ask them in the comments and I would be more than happy to answer them.
Note: This trick will not work if you use external plugins to display your ads and is only valid when you display ads by hard coding them into your themes.
so how do you show ads from outside of the loop base on post (i.e., sidebar.php).
if (is_single() && url_to_postid(get_bloginfo('url').clean_url($_SERVER['REQUEST_URI'])) != xx):
advertisements
endif;
Or you could use a conditional with the custom fields.
if ( get_post_meta($post->ID, 'show_ads', $single) == 'yes' ) {
// advertisements
}
else {
echo 'This post is special--therefore, it has no ads on it!';
}
This will only work if you have a custom field called “show_ads” and the value to show them is “yes” and if not “no”. You could also do it so if the value is nothing, then it will show them, like this:
if ( get_post_meta($post->ID, 'show_ads', $single) == '' || ! isset(get_post_meta($post->ID, 'show_ads', $single)) ) {
// advertisements
}
Just a different alternative if you don’t want to go into your site’s code over and over each time you don’t want ads.
BTW, here is a codex file on this: Using Custom Fields (WordPress Codex)
-JD Hartley
Doesn’t Who Sees Ads allow you to specify custom PHP code to determine whether or not an ad should be shown? I think you can accomplish this from within WSA instead of having to edit your theme.
The idea of using each and every ID here is a bad suggestion — it doesn’t scale at all and teaches a bad methodology of approaching this problem.
It would be much, much better to do this check based on the existence of a post meta key like “hide-ad”. This way it can be applied to any post without needing to recode the theme for every post you feel is special.
I posted that with some code examples earlier, but it is still awaiting moderation.
-JD
Who Sees Ads *does* allow showing/hiding plugin on a per post basis.
Just use your “(get_the_ID() != xx)” as the display rule.
Basically Who Sees Ads offers the same flexibility as you get when hacking files. Without hacking 🙂
Thanks for the heads up everyone, it is good to learn tricks from you all.
@Ozh thanks for confirming that this works with your plugin.
Post Meta + Some PHP Magic. Here’s what I do:
http://www.webmaster-source.co.....nse-sizes/
This is a horrible way of doing this. It’s completely non-scalable and requires editing a file every time you make a new post. Every time you edit a file you risk breaking something. I concur with the method that post #2 uses — that’s what I use in my blog. All you need to do is define the proper meta-field for the article and there is no manual editing of files needed.
As much as this tip “makes sense”, I agree with the other comments that there are better ways of doing this.
However, if you still want to “hack” the theme files, an array is more appropriate when it comes to larger lists of posts for which ads shouldn’t be shown:
$no_ads = array(
// your post ID's here in the form of 1,3,5,7...
);
/* Eg:
* $no_ads = array(1,3,5,7);
*/
if(!in_array(get_the_ID())) {
// your ad code
}
fixes & improve Frederick code
$no_ads_pid = array(6,9,42);
if (isset($no_ads_pid[get_the_ID()]){
//showads
}
that was a mistake
$show_ads = array_flip(array(1,2));
if ( isset($show_ads[get_the_ID()]) ) {
// show ads
}
Um, maybe I haven’t had enough coffee yet, but aside from the issue of whether this is the best way to do it, shouldn’t it be an AND condition instead of OR?
Say there are 2 posts that you do not want ads on — IDs 23 and 77. If you use your code:
if(get_the_ID() != 23 || get_the_ID() != 77)
You’re saying “If this post is not #23 or is it not #77, show an ad” — which means that on both 23 and 77, it will show an ad. There are 2 possible conditions under which you want no ad displayed: The state of not being 23, and the state of not being 77. The OR code requires that a post meet only one of those conditions — 23 gets an ad because it meets the condition of not being 77, and 77 gets an ad because it meets the condition of not being 23.
So if you do want to do it this way, you need the AND condition:
if(get_the_ID() != xx && get_the_ID() != xx)
That way you’re saying “If this post is not 23 and it is not 77, show an ad” — you’re requiring that it meet both conditions: The state of not being 23 and the state of not being 77.
Jamie,
Both logically work. I think most people here seem to refer using OR, but whatever. It really is all the same when it runs.
I just tested in my own test blog to make sure I wasn’t crazy (or suffering from lack of caffeine). I used the following in single.php:
if(get_the_ID() != 1 || get_the_ID() != 5) {
echo '
<div> [Your Advertisement Code] </div>
';
}
(1 and 5 are actual existing post IDs.)
And as I expected, “[Your Ad Code]” appeared on all posts, including 1 and 5.
If I change it to
if(get_the_ID() != 1 || get_the_ID() != 5)
then, as I expected, “[Your Ad]” does not appear on either post 1 or post 5, but does appear on all other posts.
Maybe I didn’t explain it very well, but seriously, try it out for yourself. The OR condition works exactly as I (perhaps incoherently) described. It’s not a matter of preference, it just will not work here. The only post that would NOT show ads if you used this code with the OR operator is a post that matches both ID numbers, and of course there is no such post, so they will all show ads.
Oops, of course on that second snippet I meant
@ChaosKaizer: Right, your first piece of code would have searched for the ID in the index; I guess your second piece of code would work.
My question is, is it more efficient to use isset() with an array key than to use in_array()?
Jamie,
It is a little late for me to be testing it out myself (I should be heading to bed sometime soon). Both logically make sense, though.
if [id] doesn’t equal 1 OR if [id] doesn’t equal 5
If ID equals 2, it will be true. If it is 1, it still will return as false because of the OR statement.
The exact same thing will go for if it is an AND statement. I am not sure why it isn’t working how it is suppose to on your install. I could be wrong, of course, but it doesn’t make sense to me why this isn’t working correctly. Maybe it’s just me….
Anyone else have any insight on this? Maybe a third opinion will help clear it up.
-JD
Both logically make sense, though.
I’m not trying to be awkward or obnoxious here, but no, they don’t. If they did, why would two different operators exist? OR and AND don’t mean the same thing.
if [id] doesn’t equal 1 OR if [id] doesn’t equal 5
Right, think about that. If it doesn’t equal 1 or it doesn’t equal 5. 1 doesn’t equal 5, so it meets one of the two OR conditions. Since the entire definition of OR is that it only needs to meet one of them, an ad will display.
OR would only work here if you were trying to use positive conditions — i.e. “if ID does equal 1 or it does equal 5, then don’t show an ad”. (Conversely, if you were doing it that way, you could not use AND. They’re not interchangeable.)
As other people have pointed out, there are probably better ways of doing this, but all I’m trying to show is that if users see this post and use the code Keith provided, it won’t work.
Hmmm….alright. That does make more sense now.
I have tried this code to my single.php but it is not working in either way i dont know what is wrong with this can anyone help me to solve out this issue..