public
Created

Plugin for random posts shortcode

  • Download Gist
jl-random-posts.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
<?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]
*/
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.