WordPress hack : How to display ads on old posts only

To achieve this recipe, simply open your functions.php file and paste the following code in it:

function is_old_post($post_id=null){
$days = 15;
global $wp_query;
if(is_single() || is_page()) {
if(!$post_id) {
$post_id = $wp_query->post->ID;
}
$current_date = time();
$offset = $days *60*60*24;
$post_id = get_post($post_id);
$post_date = mysql2date(‘U’,$post_id->post_date);
$cunning_math = $post_date + $offset;
$test = $current_date – $cunning_math;
if($test > 0){
$return = true;
}else{
$return = false;
}
}else{
$return = false;
}
return $return;
}

Once done, you are now ready to call the functions in your single.php template as shown below:

<?php if(is_old_post()){ ?>
INSERT AD CODE HERE
<?php } ?>

That’s all, you’re done. By default, ads will be displayed on post older than 15 days. To change this value, simply edit the $days variable on line 2.
This recipe has been initially published on my other blog Cats Who Blog. If you haven’t yet, you shoul definitely visit…

You Might Also Like...

  • Top WordPress hacks of 2009

    1468 readers2009 has been a very prolific year for WordPress hacks. In this article, I'll show you the most useful hacks I came across during the whole year. Enjoy!Have you checked out the highly recommended Digging into WordPress book by Chris Coyier and Jeff Starr?Top WordPress hacks of 2009

  • Exclude A Category From Turn Off Comments Automatically

    59 readersThanks to our post New Ideas For 2010? Now It’s Your Turn! we got an email form one of our loyal sponsors Chris Coyier. Hey WP Engineer folks! I have a little issue I'm trying to resolve on CSS-Tricks, regarding comments. In my discussion settings, I have it set to automatically turn off comments on

  • How to display your average feed readers

    696 readersMany bloggers are using Feedburner "chicklet" to display their subscribers count. If you want to be able to display the count for an average 7 days, here is a piece of code you're going to love.Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!How to

  • List Posts by Category In Navigation

    55 readersWordPress is primarily a blog platform. Not infrequently it is used as a CMS. But not just static pages play a role, but also posts, because they are giving more opportunities, having better performance and be published in feeds. Therefore, I prefer to use, in the context of WP as a CMS, posts more than

  • Leave Comments Open Forever on Specific Categories or Page Templates

    1663 readersI like the idea of shutting off comments after a certain number of days. Here on Digging Into WordPress we do it after 90. After that kind of time, the “community” of the discussion is long over. I think a good practice for turning off comments is to instead leave a message informing visitors that

  • Introducing CatsWhoBlog.com

    208 readersAlmost one year after successfully launching WpRecipes.com, I am extremely happy to introduce you my new blog, 100% dedicated to blogging tips and named CatsWhoBlog.com.Have you checked out the highly recommended Digging into WordPress book by Chris Coyier and Jeff Starr?Introducing CatsWhoBlog.com

  • Disable widget areas (sidebars) without touching theme templates

    510 readersA guide on how to remove WordPress widgets areas on a conditional basis with sacrificing the original theme code.

  • How to: Offsets and Paging

    325 readersReader John writes in: How does one use paging with an offset? Doing so breaks the navigation controls. The problem with using an offset in a query is that WordPress ignores any reference to paging. In other words, you can use an offset and paging, but not both together. This can be solved by tapping

  • 10 jQuery snippets for efficient developers

    659 readersjQuery is by far my favorite Javascript framework, which allows developers to create stunning visual effects, manipulate data properly, and much more. In this article, I have compiled 10 extremely useful jQuery snippets.Have you checked out the highly recommended Digging into WordPress book by Chris Coyier and Jeff Starr?10 jQuery snippets for efficient developers

  • Top WordPress hacks of early 2010

    2435 readersThe first months of 2010 have been extremely prolific in terms of WordPress hacks. In this article, I have compiled 10 new WordPress hacks that you should definitely add to your library.Like CatsWhoCode? If yes, don't hesitate to check my other blog CatsWhoBlog: It's all about blogging!Top WordPress hacks of early 2010