I want to show different content on each page refresh. To accomplish this, I've used the PHP array_rand()
function.
I fetched posts and stored content in a value using the below code. When I output the value before array_rand()
, it is showing first post content but in array_rand()
if I output the random functionality is working but showing some random number. I believe it's post count. The minimum num shown is 0 and max is 2 there are 3 posts.
Check this here and the code I used is.
<?php
$args = array(
'posts_per_page' => '-1',
'order'=> 'ASC',
'orderby'=>'menu_order',
'paged' => $paged,
'post_type' => 'Didyouknows'
);
$loop = new WP_Query( $args );
$posts = $loop->get_posts();
foreach($posts as $post){
//print_r($loop);exit;
//echo $loop->['ID'];
$rval[]= $post->post_content;
//print_r($rval);exit;
//echo $loop[post_content];
//print_r($rval);exit;
$rand = array_rand($rval);
//echo $rval[$rand[0]];
//echo $rand;
}
?>
<h2><?php echo $rand; ?></h2>
Where am I going wrong?