How to pull a random post from the archives onto the homepage?
-
I’m using the TwentyTen theme and am trying to figure out how to have the homepage show a random post from the archive. So every time someone visits the homepage, they would see a different post, randomly pulled from the archive of posts.
What I’ve done so far is limit the homepage to one post by using
pre_get_posts
in functions.php. This is the snippet I added at the end:function special_home_pagesize( $query ) { if ( is_admin() || ! $query->is_main_query() ) return; if ( is_home() ) { // Display only 1 post for the original blog archive $query->set( 'posts_per_page', 1 ); return; } } add_action( 'pre_get_posts', 'special_home_pagesize' );
This ensures that my archive, tags, category pages still show 10 posts per page, but limits the home page to one post.
Now I’m trying to figure how to generate a random post to appear on the homepage each time someone loads the page…
Here’s the code I’m using for index.php
And here’s the code I’m using for loop.phpCan anyone point me to which lines I need to change?
Thank you!
- The topic ‘How to pull a random post from the archives onto the homepage?’ is closed to new replies.