I have worked out how to select a random item - add a sort criteria of type Global: Random. I can also select from, say, the last month, by adding a filter. However, if I want to select randomly from the last, say, 5 items posted, I can't find a way to do that.

  • Do you want to pick 1 random value from the last 5 item posted?
    – Emil Orol
    Commented Nov 30, 2015 at 16:45

1 Answer 1


Sort your View by Content: Post date (desc) and set the Pager to Use pager: Display a specified number of items | 5 items

enter image description here

Go to the Advanced Settings in the view and click in the Theme: Information

enter image description here

Search the Style output: section and select a name, in my case was views-view-unformatted--temp.tpl.php

Go to the folder YOUR_SITE/sites/all/modules/views/theme and copy to your theme folder the file views-view-unformatted.tpl.php and rename it to views-view-unformatted--temp.tpl.php

Edit your new file views-view-unformatted--temp.tpl.php and put inside only this code to show only one element:


 * @file
 * Default simple view template to display a list of rows.
 * @ingroup views_templates
 //Selecting one element from your last 5 posted elements
 $element = $rows[rand(0, 4)];
 $rows = array();
 $rows[] = $element;

<?php if (!empty($title)): ?>
  <h3><?php print $title; ?></h3>
<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
  <div<?php if ($classes_array[$id]) { print ' class="' . $classes_array[$id] .'"';  } ?>>
    <?php print $row; ?>
<?php endforeach; ?>

In the View go to the bottom of the Information window and click Rescan template files and click OK, then save your View.

enter image description here

And go to see your result.

Attention: Remember to repeat these steps if you ever change theme.

  • @James thanks James, a pleasure help you ;-) Commented Dec 1, 2015 at 16:17
  • I don't suppose you know how to get this to work with Drupal 8 - it doesn't have the "Theme: information" option in Views.
    – James
    Commented Dec 1, 2015 at 18:27
  • 1
    @Pierre.Vriens thanks I edit now and go to the bed, here is 1:05am ;-) Commented Dec 11, 2015 at 6:04
  • 1
    @James try using the Drupal 8 Views Templates module
    – No Sssweat
    Commented Dec 16, 2015 at 18:17
  • 1
    @NoSssweat thanks ;-) I get the point +1 for you ;-) Commented Dec 16, 2015 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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