0815

Du musst dich anmelden um Beiträge und Themen zu erstellen. Einloggen · Registrieren

Easy Random Posts

Newer versions of WordPress enable easy randomizing of posts by using the (relatively) new “orderby=rand” parameter in any query_posts loop:

<?php query_posts('orderby=rand"); ?>

But for older versions of WordPress, this randomizing functionality must be added manually. Here is a quick and painless plugin that will enable you to randomize post queries in older versions:

<?php
/*

Plugin Name: Random Posts Query

*/

function query_random_posts($query) {
	return query_posts($query . '&random=true');
}
class RandomPosts {
	function orderby($orderby) {
		if (get_query_var('random') == 'true')
			return "RAND()";
		else
			return $orderby;
	}
	function register_query_var($vars) {
		$vars[] = 'random';
		return $vars;
	}
}
add_filter( 'posts_orderby', array('RandomPosts', 'orderby') );
add_filter( 'query_vars', array('RandomPosts', 'register_query_var') );
?>

Once activated, invoke the randomness by adding the “random=true” parameter in your query_posts loop:

<?php query_posts('cat=11&showposts=11&random=true'); ?>

Here, we are specifying “cat=11” for the category and “showposts=11” for the number of posts to display. There are many other parameters available as well, so knock yourself out.

Credit: rembem




0