Random Nodes with Views?

nofue - August 11, 2006 - 02:05

Servus, folks.

I want a floating block showing a random quote. I can't figure how to get a random selection from these CCK records called 'quotes' using the view.module... I found some notes that the former "random node" option was dropped in spring because of some new feature, but I couldn't find any further hint. The online help and some docs still say chosing "random node" should do the trick, but that's gone now.
So how do I select a random record from CCK using view?

TIA,

Norbert

Random

Richard Eriksson - August 16, 2006 - 18:24

Really? I have it on one of my sites, which has the latest version of the module for 4.7. Here's how I did it (instructions may be verbose, but this may be useful for those new to Views and searching for how to display a random post in a block):

  1. create a new view in administer » views » add tab.
  2. give it a name. I called mine "Random".
  3. click the "Block" section heading to get its settings.
  4. enable "Provide block"
  5. choose a "View type". I went with "List View" so that I could choose which fields display in the block.
  6. type in a title. This will be the title of the block.
  7. type in a number for the amount of nodes you want to display in the block. You can use 1 (number one) if you like.
  8. click the "Fields" section heading to get its settings.
  9. add the fields you want. I chose just "Node: Title" since I wanted just links to the nodes, not the node text themselves. You should be able to choose whichever field has the quote in your CCK node.
  10. click the "Filters" section heading to get its settings.
  11. select "Node: Type" and click "Add Filter"
  12. choose the CCK node type(s) you want to appear in the block.
  13. select "Node: Published" and click "Add Filter". (I keep forgetting this one, but it's important, since you probably don't want to display unpublished nodes.
  14. click the "Sort criteria" section heading to get its settings.
  15. select "Random" and click "Add criteria".
  16. click "Save"
  17. click administer » blocks and enable the block you titled in step #6.

Thanks for the tip

trevortwining - August 16, 2006 - 19:09

That was on my list of things to figure out this week. Scratch it off now! Thanks a bunch.

Trevor Twining
http://www.trevortwining.info

Thanks a million

nofue - August 17, 2006 - 12:48

Works like a charm -- you made my week.

Norbert

-- form follows function

Me Too!

rich.yumul - April 5, 2007 - 22:59

I just wanted to add my thanks for this little how-to. THANKS!

Rich Yumul
www.sagetree.net

Simplified

Richard Eriksson - September 2, 2006 - 07:21

I simplified this how-to a little, adding the PHP that views exports to save a little time, in a handbook page for linking to a random blog post in a block.

This works extremely well -

chrissearle - September 20, 2006 - 20:29

This works extremely well - with one tiny little problem (there's always one who complains isn't there).

The block section of a view allows for checking a checkbox which will then go to the page view of the same view. This is useful - it keeps the same filters etc etc - but - of course it also keeps the random sort.

Is there any approach which will allow for the block sort order to be random but to get a link to a page view where the sort is something else?

I'm wondering about contemplate module and manually adding the link in the template (having two views, one for the block, one for the page) - but is this the right way?

I did this as well because

kweisblatt - September 24, 2006 - 20:03

I did this as well because of a few issues with contemplate recently. But, I didn't see the "random" option which is great! I want it to look more like a teaser and give the view a length so it will cut off after so many lines. I tries changeing the block height but it didn't work. Any ideas?

~~~~~~~~~~~~~~~~
Kris
Current project: www.cribfax.com

one of the best tutorials I've seen

thomasmuirhead - February 19, 2007 - 19:12

Thanks Richard. Generally people aren't willing to make a step by step tutorial for something that seems so simple. It helps so much. Thank you.

Excellent step by step

pharma - April 24, 2008 - 02:36

Excellent step by step guidelines

Views2

binford2k - July 25, 2008 - 18:07

In views2, simply add the Sort criteria of Global:Random

dude, you da man (or woman, not sure)

dgoldenbaum - August 28, 2008 - 21:34

thanks for the Views2 addition. big help.

coolio

davenewton - August 11, 2008 - 11:09

just used this method on a page view, cheers dude

Automatically refreshes after x seconds?

asfahaan - May 20, 2009 - 23:24

Excellent tutorial works really well.

Just wanted to know:

Is it possible to make the block refresh via javascript or some other means? E.g. every 5 seconds another random quote appears, etc... ? Is this possible wiht viewS?

=-=

VeryMisunderstood - June 11, 2009 - 18:10

investigate the views cycle module or one of the other views addons that may do what you ask.

top work!

carlito - April 24, 2008 - 05:01

thanks richard, works a treat :)

I've just tried this method.

e3ekiel - October 14, 2008 - 17:02

I've just tried this method. It works fine when im logged in as admin, but when im logged out and viewing the page as any normal person would the random function doesnt work.

Any ideas?

exclude current node from view

gost_ - February 20, 2009 - 01:05

and don't forget that it is always a good idea to exclude current node, so the page you are viewing the random block on doesn't show up in your block!

Drupal 5 http://drupal.org/node/131547

Drupal 6 requires no code! another reason to switch :)

To exclude the current node from the view:

1) Add another argument = Node:Nid
2) Select "Action to take if argument is not present" = "Provide default argument"
3) Select "Default argument type" = "Node ID from URL"
4) Select "Exclude the argument"

source:drupaleasy.com

This is very

izmeez - February 20, 2009 - 01:11

This is very helpful,

Thanks,

Izzy

Bookmarking, anyone knows how

Summit - March 3, 2009 - 11:19

Bookmarking, anyone knows how to deal with this using the taxonomy term as argument, so showing random nodes in a block related to the term in the url (taxonomy/term/%) ?

Thanks a lot for your reply!
Greetings,
Martijn

Different Color Node and Block Question

Gary Coleman - June 11, 2009 - 18:11

I have a tricky question
Is there any way to make the randomizing content-type a block instead of a node?

Because my blocks are grey and my nodes are white with rounded edges
When I follow these instructions my grey block shows the randomizing article nodes with the node with the white background inside it

http://img197.imageshack.us/img197/9240/croppedoffadproblem.jpg

It also pushes the ad off the right edge of the page because of the left margin space
And it puts the node comment count below it which is no good

In blocks I can copy and paste the ad html and it fits perfectly centered and doesn't put a different background color, but I don't know how to randomize it

Is there any way to randomize ads in a block with views?
Or is there a way to set the content-type to be a block instead of a node?

That's a styling question,

binford2k - June 15, 2009 - 18:56

That's a styling question, not a Views question. Install firebug, then inspect your block. It will quickly become obvious how to style the block to achieve what you want.

That said, there are several ad managing modules here, which depending on context may already do what you want. You might check to make sure you're not re-inventing the wheel.

 
 

Drupal is a registered trademark of Dries Buytaert.