Random image from a particular category/gallery

Last modified: April 5, 2009 - 20:29

Here's a block that works like random image, but pulls the images from a chosen gallery/category.

<?php
  $gallery_name
='Enter the name of the gallery here';
 
$term = taxonomy_get_term_by_name($gallery_name);
 
$tid = $term[0]->tid;
 
$thumbs = 0;
 
$images_arr = (image_get_random($count = $thumbs, $tid));
  foreach (
$images_arr as $images){
      print
l(image_display($images, 'thumbnail'),'node/'.$images->nid, array(), null, null, FALSE, TRUE);
  }
?>

An alternative version slightly modified to display only the image. This function just shows the image without any links.

<?php
  $gallery_name
='Enter the name of the gallery here';
 
$term = taxonomy_get_term_by_name($gallery_name);
 
$tid = $term[0]->tid;
 
$thumbs = 0;
  while (
$thumbs<1)
  {
   
$images = (image_get_random($count = 1, $tid));
    print
image_display($images[0], 'preview');
   
$thumbs++;
  }
?>

 
 

Drupal is a registered trademark of Dries Buytaert.