I have a need to pull all entries (in this case only 20 or so) from a channel, and have 5 random results do something different to the other 15. What's the best way to achieve this?

  • 1
    Do you want to list 20 entries in random order and display the first 5 differently or do you want to output 20 and randomly treat 5 of them differently? Dec 3, 2012 at 16:57
  • The latter. Basically, we want a grid of results (3 columns, x rows) and every so often one of the results is to display a thumbnail image. Completely randomly. Not my idea :) Dec 7, 2012 at 12:20

2 Answers 2


I'm not sure there is a plugin already written that does this, but it'd be pretty easy to write one or, use PHP in the template to do the same thing.

Basically, you want to generate a list of entries to be selected first, outside the {exp:channel:entries} tag pair.

Then, inside the tag pair, you'll just check to see if the current {count} is in the array.

Something like this should work:

   $max = 20;
   $numels = 5;
   $range = range(0, $max);
   $vals = array_rand($range, $numels);

{exp:channel:entries ... }
   <?php if(in_array('{count}', $vals)):?>random-entry<?php endif;?>
  • That's a better way than the one I used, and pretty obvious too. Yet I could not think of it myself. Dec 5, 2012 at 20:13

I did this in the end like so:

$randoms = array();
{exp:channel:entries channel="styleguide" disable="categories|category_fields|member_data|pagination|trackbacks" dynamic="no" limit="5" orderby="random"}
array_push($randoms, '{entry_id}');

{exp:channel:entries channel="styleguide" disable="categories|category_fields|member_data|pagination|trackbacks" dynamic="no" orderby="title" sort="asc"}
        if (in_array('{entry_id}', $randoms)) {
            echo 'hoorah';

Except I removed the first entries pair and replaced it with a simple SQL query.

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.