How to set expiration date for a post using custom fields?
Custom fields provide a strong tool for developers to make it more user friendly and customizable.
Today, we will see how we can assign an expiration date to a post, so the post will disappear after a particular date:
1) Change the default loop in your index.php file as mentioned below:
<?php
if (have_posts()) :
while (have_posts()) : the_post(); ?>
$expirationtime = get_post_custom_values(‘expiration’);
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}
$secondsbetween = strtotime($expirestring)-time();
if ( $secondsbetween > 0 ) {
// For exemple…
the_title();
the_excerpt();
}
endwhile;
endif;
?>
2) Now, when you add/edit a post, add a custom field expiration and assign the date as value(with the format mm/dd/yyyy 00:00:00). The post will disappear after that date from the home page.
Note that if you do not assign the custom field to a post, that post will not appear on your home page. This code needs some modification for the same.
You may like these articles :
- Introduction to WordPress Database: wp_postmeta Introduction to WordPress Database: wp_postmeta Current Version : 2.9.2 Just...
- Using gravatar for the post author in wordpress posts Using gravatar for the post author in wordpress posts: We...
- Delicious starts using Twitter to rank bookmarks on home page Delicious, the Yahoo-owned social-bookmarking site, has awoken from its slumber,...
Jesse Rosenfield
There is no code displaying at “1)” where it says Change the default loop in your index.php file as mentioned below: