How to add a random post button in WordPress

by teachyourselfwebsites.com

I used this method to add a “random post” to this site. I didn’t create a child theme. So it will break when the theme gets updated.

http://teachyourselfwebsites.com/create-random-post-button-wordpress-blog/

4/24/2016 note:

Here are some details. You need to create a new file inside the theme called page-random.php based on the original page.php.

the added code looks like this and goes right before get_header():

 

<?php
/* Template Name: Random Post */

$randomPost = get_posts(array(
 'numberposts' => 1,
 'orderby' => 'rand'
));
 
foreach ($randomPost as $post) {
 wp_redirect(get_permalink($post->ID));
 exit();
}