Created
February 24, 2012 22:39
-
-
Save JLeuze/1904267 to your computer and use it in GitHub Desktop.
Plugin for random posts shortcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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