Getting a single random post would be far more efficient than getting all posts, then plucking a random post from the query. Here’s an updated WP_Query instance using 'orderby' => rand
:
$args = array(
'orderby' => 'rand',
'posts_per_page' => '1',
'paged' => $paged,
'post_type' => 'Didyouknows', // Post type names should never use capital letters, btw
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) { $loop->the_post(); ?>
<h2><?php the_content(); ?></h2><?php
}
}
wp_reset_postdata();
In your original code, you are seeing the values of 0, 1, and 2 which represent the possible keys for elements within your $rval
array. You should move $rand = array_rand($rval);
out of the foreach
loop and do something like:
<h2><?php echo $rval[ $rand ]; ?></h2>
The fist solution using WP_Query
and 'orderby' => 'rand'
is preferable though.