I'm using Drupal Cycle module and list field_images items. I check 'random' button, but it is not working. I want get random image every refresh page. How can do it? field_sort_items or anyway solution?

  • 2
    In the view the sort criteria should be global: random – Tyler Durden Dec 20 '12 at 17:21
  • I don't use Views. – Karmacoma Dec 20 '12 at 22:44

You can do this in lots and lots of ways. This is 1 way without Views.

I did this for a homepage. I made 5 variables that hold FIDs for files uploaded using FAPI file_managed fapi element in an admin settings form.

Then using hook_page_build if I'm on the homepage I created a render array that simply calls theme_image using a random file id by loading my 5 variables into an array and using PHP's array_rand() to extract 1 random array value.

Lastly, if you can get this far -- and you have page caching turned on ... you'll only get 1 image ever and it'll never be random. For the render array inserted using hook_page_build you have to set #cache => FALSE within the render array so Drupal will not cache your images and it will be random every pageload.

| improve this answer | |

If you used views, you might try Views Javascript Random module which randomizes on the client side. Sadly, it looks like it is only for D6.

Adds a new sort type to views. It's under sort criteria -> Global: Javascript Random.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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