Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Plugin for random posts shortcode
Plugin Name: JL Random Posts
Plugin URI:
Description: Load a list of random posts.
Version: 1.0
Author: Josh Leuze
Author URI:
License: GPL2
/* Copyright 2012 Josh Leuze (email :
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
function jl_random_posts() {
// Settings for random list loop
global $post;
$jlrandom_posttemp = $post;
$jlrandom_output = '';
$jlrandom_loop = new WP_Query( array(
'posts_per_page' => 3,
'orderby' => 'rand'
) );
// Loop which generates the list
while ( $jlrandom_loop->have_posts() ) : $jlrandom_loop->the_post();
$jlrandom_output .= '<li><a href="' . get_permalink() . '">' . the_title( '', '', false ) . '</a></li>';
// reset the random list loop
$post = $jlrandom_posttemp;
// output the list
return '<ul>' . $jlrandom_output . '</ul>';
/* To load the list, add this line to your theme:
<?php if( function_exists( 'jl_random_posts' ) ) { echo jl_random_posts(); } ?>
// Adds shortcode to load list in content
add_shortcode( 'jlrandom', 'jl_random_posts' );
/* To load the list, add this line to your page or post:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment