Controlling Ads in WordPress: Older Posts Only
A friend of mine on a forum was curious on how to make the in-text popup advertisements only show up on older posts. Kontera, among others, supply those advertisements that are special links that show a content box when you hover over them. He found it too intrusive for his everyday readers, and so only wanted to show it on posts older than a week.
I had a script handy which I showed him, and it worked out fine. I am not saying this is the best script and everyone should use it, but it is my solution. If you have a better one, as always, I'd love to see it.
-
<?php
-
/*
-
Show your in text ads on posts older than 7 days.
-
Put this around the Javascript they give you.
-
*/
-
-
-
if($days_since>= 7) {
-
$older=1;
-
}
-
-
if ( is_single() && $older==1 ){ ?>
-
<!-- Put your JS code from the company here -->
-
-
<?php } ?>
Basically the script says, look at today's date. Look at the post we are on and its date, and if it has been more than seven days, show the advertisement JavaScript.
You could use this code for all sorts of things. I have used it to show larger advertisements on older posts, as well as part of a script that shows advertisements only to those that are from search engines. It can be very useful to be able to manipulate WordPress in these ways.
Let me know what you think, especially if I have messed up. And I hope this helps those of you that have been wondering how to do this with WordPress and without any sort of plugin.




Want an avatar? Get a gravatar! • You can link to this comment
That is pretty clever. I will try something like that on my blog, I think. Food for thought, at any rate.
Want an avatar? Get a gravatar! • You can link to this comment
Here’s the ColdFusion code to do the same thing:
Want an avatar? Get a gravatar! • You can link to this comment
That is is some handy code!!