down arrowMenu

UNI Webtools: Resources for Web Developers

Creating a random image block with views

Posted on Tuesday, June 16th, 2009 by Anonymous

One interesting use of the views module that I came across is creating an image block that loads a random image whenever the page is refreshed or a user navigates throughout the site.  This method will rely on Views, as well as CCK and the Imagefield module.

The first step is to create a new content type specifically for the images in your block.  Under "Manage Fields" for this new content type, add an image field and configure the options for it to suit your needs.

Next you will need to create a new view for displaying your images.  On the configuration screen for your new view, begin by adding a block display.  There are several sections that need to be edited now to get your block to display a single random image.

1) Change the "Items to display" number to 1.

2) Under fields, set Content: Image as the only displayed field (this field will show up as an option only after you have made your custom content type with the new image field)

3) Under filters,  set Node: Published and  Node Type = "whatever you named your content type" as the two filter options

4) Lastly, under Sort Criteria, set Global: Random as the method of sorting.

This view will only target image fields from that one specific content type, and then arrange the images in a random order.  Since it is set to only actually display 1 image, you now have a very simple random image generator.

After saving your view, you can now just go to Site Building --> Blocks and place the new block in whatever region you want.