Skip to content

@JLeuze /jl-random-posts.php
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Plugin for random posts shortcode
<?php
/*
Plugin Name: JL Random Posts
Plugin URI: http://www.jleuze.com/plugins/jl-random-posts/
Description: Load a list of random posts.
Version: 1.0
Author: Josh Leuze
Author URI: http://www.jleuze.com/
License: GPL2
*/
/* Copyright 2012 Josh Leuze (email : mail@jleuze.com)
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
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
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>';
endwhile;
// reset the random list loop
$post = $jlrandom_posttemp;
wp_reset_postdata();
// 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:
[jlrandom]
*/
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.