OK, i wanted my page to post a random post in every 24 hours , so i found that code over the internet,
I was trying to understand the code and added the comments to those lines which i understood, however i am not able to understand the complete program, what is get_transient and set_transients are :?
and What if i want to show random post from category whose id is 1 on Friday ?
<?php
if ( false === ( $totd_trans_post_id = get_transient( 'totd_trans_post_id' ) ) ) {
$args = array('numberposts' => 1, 'orderby' => 'rand'); // Generate Random Post
$totd = get_posts($args); // Get the Random post generated earlier
$midnight = strtotime('midnight +1 day'); // Get the Time of next day from Today's Midnight
$timenow = time(); // Current Time
$timetillmidnight = $midnight - $timenow; // subtracting current time from Midnight (Time remaining in midnight)
set_transient('totd_trans_post_id', $totd[0]->ID, $timetillmidnight);
} else {
$args = array('post__in' => array($totd_trans_post_id));
$totd = get_posts($args);
}
foreach( $totd as $post ) : setup_postdata($post); ?>
<div>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php the_content(); ?>
</div>
<?php endforeach; ?>