Random post on front page
-
I’m trying to display one random post on the front page.
I put this code before The Loop and it works well, except it doesn’t display comments or split posts with More tag.
<?php $rand_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY RAND() LIMIT 1"); query_posts("p=$rand_id"); ?>
I also tried this code, but it didn’t work at all. Maybe I just didn’t implement it correctly.
<?php global $wpdb; $numposts = 1; $rand_posts = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_status = 'publish' ORDER BY RAND() LIMIT $numposts"); foreach($rand_posts as $post) : setup_postdata($post); ?> ... <?php endforeach; ?>
Can someone please tell me how to get either one of these working correctly?
Thanks
- The topic ‘Random post on front page’ is closed to new replies.