Random post that will last for a day – daily post
-
How do I create a random post that will last for a day, then go to another post. The post should come from a specific category, not just any one.
Below is what I have tried and it rotates for that daily period, though it takes the post from any category, not from a specific category.
‘<?php
$args = array(‘numberposts’ => 1, ‘category’ => 3,’orderby’ => ‘rand’);$lastposts = get_posts( $args );
if ( false === ( $totd_trans_post_id = get_transient( ‘totd_trans_post_id’ ) ) ) {
$totd = get_posts($args);
$midnight = strtotime(‘midnight +1 day’);
$timenow = time();
$timetillmidnight = $midnight – $timenow;
echo $midnight;
echo “,”.$timenow;
set_transient(‘totd_trans_post_id’, $totd[0]->ID, $timetillmidnight);
} else {
$args = array(‘post__in’ => array($totd_trans_post_id));
$lastposts = get_posts($args);
}foreach( $lastposts as $post ) : setup_postdata($post); ?>
<div>
<?php the_content(); ?>
</div>
<?php endforeach;
wp_reset_postdata(); ?>
‘Any help would be appreciated.
- The topic ‘Random post that will last for a day – daily post’ is closed to new replies.