Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have my standard category page. On this page, I'd like to put a button which, when clicked, randomises the results shown on the category page.

I know I could make a rand-category.php page and send the user on to that page. Ideally though, I'd like a more elegant solution, possibly something that refreshes the results without moving the user on/away from the page.

share|improve this question
    
You only want to shuffle the posts he sees on the current page? Or do you want to shuffle all subpages for the category archive? –  Kau-Boy Aug 20 '12 at 17:31
    
I want to shuffle all subpages from the category archive. –  Olly F Aug 20 '12 at 17:34
    
Than you will need to have a different permalink for those pages. So a custom tempalte would be the easiest solution. But if you want to "preseve the shuffle result" on the subpages, you have to store the result in the user's session and use it so sort the posts. –  Kau-Boy Aug 20 '12 at 17:36
    
Oh, I don't think I need to preserve the shuffle results. The idea is that when the user is viewing content on our category page, we'd like a random button that allows other content (say from many years ago) to be surfaced. So it's just a temporary measure for the user, so they get the opportunity to see results that they wouldn't normally. –  Olly F Aug 20 '12 at 17:39
2  
Another easy solution would be a ajax request with new posts for the current page. That is fairly easy using jQuery. And it will allow the user to get more shuffled results even quicker. –  Kau-Boy Aug 21 '12 at 7:17

2 Answers 2

You can actually change any of the query parameters (like the order) by adding them to the url.

For instance, the sample url below would randomize the posts on the category archive every time you refreshed, randomizing the results.

http://sample.com/category/uncategorized/?orderby=rand

Below is a basic form with the orderby value that would refresh whatever page/template it shows on. (you would just need to add it to archive.php in your theme, or to the sidebar via an HTML widget)

<form method="get">
<input type="hidden" name="orderby" value="rand" />
<input type="submit" value="Randomize" />
</form>

You would just need to work out the ajax to refresh the HTML element holding your loop. (the guy over at this post is doing a ajax comment refresher, you could look at how he is reloading his loop via ajax

share|improve this answer

Simple solution I guess :

make a button/link with url to same page locationn (ex for page "Our Page": Random results )

modify the query args to randomise the results

$query = new WP_Query( array ( 'orderby' => 'rand') );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.