4

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.

5
  • 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?
    – 2ndkauboy
    Aug 20, 2012 at 17:31
  • I want to shuffle all subpages from the category archive.
    – Olly F
    Aug 20, 2012 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.
    – 2ndkauboy
    Aug 20, 2012 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, 2012 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.
    – 2ndkauboy
    Aug 21, 2012 at 7:17

2 Answers 2

1

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

0

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') );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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