/ Published in: PHP
This is an updated version of the original Wordpress post expiration code (<a href="http://snipplr.com/view/3899/wordpress-post-expiration-code/">3899</a>) that bugs out towards the end/beginning of a month when using European date format (d.m.Y). Usage is the same, this code makes "expired" posts cease to show up in the normal page, but they should still show up in the archives. When writing the post, enter a custom field with the key "expiration" and set the date. The below code will go on the index page in the theme folder.
I've changed the way dates are compared using <a href="http://www.highlystructured.com/comparing_dates_php.html"> this article</a> for reference.
Anomalies handling:
- If there is no expiry date set, a post will show up until 2050.
- If the expiration field has a value that can't be converted to Unix timestamp, it will be empty and thus smaller than today's date, i.e. the post won't show up on the home page.
I've changed the way dates are compared using <a href="http://www.highlystructured.com/comparing_dates_php.html"> this article</a> for reference.
Anomalies handling:
- If there is no expiry date set, a post will show up until 2050.
- If the expiration field has a value that can't be converted to Unix timestamp, it will be empty and thus smaller than today's date, i.e. the post won't show up on the home page.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
***PUT THIS INSIDE THE LOOP, FIRST THING*** <?php //to check against expiration date; $expirationdate = get_post_custom_values('expiration'); $expiration_date = '2524608000'; //posts without an expiration date show up until 2050; } else { } if ($expiration_date > $today) { $valid = "yes"; } else { $valid = "no"; } if ( $valid == "yes" ) { ?> ***THEN PUT THE FOLLOWING LINE AT THE VERY END OF THE LOOP*** <?php } //end if for expiration; ?>