Community Documentation

Random image from a particular category/gallery

Last updated April 5, 2009. Created by jbrauer on March 14, 2006.
Edited by ghoti, add1sun, sepeck, sammos. Log in to edit this page.

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++;
  }
?>

Comments

for 6-x

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

is it possible...

do include only showing images that have a certain 5-star rating? or perhaps images that are rated 3 stars or higher so that the random images are chosen from the better quality images?

this would be incredibly useful....

styling

How to put html tags around the images so I can style them?

How does this work exactly?

I can't get it to work-
I create a block, enable PHP input- paste the following code, except I replace the name of the gallery in the right area.

<?php
  $gallery_name
='The People';
 
$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);
  }
?>

But dosent work.

Hi, I have exactly the same problem: created a new block with following php code but it's not visible anywhere:

<?php
  $gallery_name
='Progettazione Hotel';
 
$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);
  }
?>

What else do I have to configure or add? I'm using Drupal 6 and Image module 6.x-1.0.

Thanks for your support.

PHP filter

It works on Drupal 6! You must enable the module PHP filter, then create a new block pasting the above code and selecting PHP Code as input format.

About this page

Drupal version
Drupal 4.7.x, Drupal 5.x, Drupal 6.x

Reference

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here