Hi,
I found this plugin in a mailinglist of wp hackers somewhere:
<?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') );
?>
Install this as a plugin, activate it, and then place this above the loop (note the random=true at the end:
<?php
query_posts('cat=22,3,2,5&showposts=3&random=true'); ?>
It works well for me!
thanks rembem, you save me a lot of time;))) it works very well, finally easy to use and customize random plugin
It must be something obvious but I can’t find it ! So I call some help. Sometimes you are searching your glasses and they are on your nose, isn’t it ?
I have made a dropdownmenu witch list each category on my blog with :
<? wp_list_cats(‘sort_column=name&exclude=7’); ?>
When you click on “Books”, you arrive on the category page. This one obeys to the archive template and so display the list of all the posts in this particular category. Easy until this point.
My problem is that I want too to display 2 posts (with title, texte and all stuff) of this category below this list… randomly !
So I have installed this wonderfull plugin (works like a charm) : http://wordpress.org/support/topic/70359?replies=3
And I have put like it is advised this line in my archive template :
<? query_posts(‘cat=&showposts=2&random=true’); ?>
2 posts are displayed randomly… but I can’t find how to indicate to the query that she must take these posts in the current category… and I don’t want to make 11 different templates… There must be a solution.
I have try is_category() whithout success, some pieces of code about current category in the codex and it doesn’t work. I have search on the forum and found nothing about my problem.
Is there someone to save me and the world ?
THanks in advance for your attention.
Pixelboss, I guess you don’t need a plugin to do it.
You could use the WordPress get_posts() function and use the orderby parameter do use a random value.
Take a look at this code:
get_posts('numberposts=4&category=3&orderby=rand()');
Regards,
Caio Proiete
http://www.pdaexpert.net
I made a plugin for a buddy of mine that displays random posts by category.
Basically, it has three functions, can display a set number of posts that belong to two distinct categories(eg only displays posts that belong to category 1 and category 5), display a set number of random posts that belong to two distinct categories, or a set number of random posts that belong to a single category.
http://www.ax697.org/46/category-magic-plugin/
Maybe this can help, not sure.
oh, my god, finally the solution to what I’ve wanted to get done… pick a category and show a limited number of posts (most recent or random) from it in the format of my choice!