2

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?

2
  • 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? – Kim Wickell Dec 3 '12 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 :) – Andy Harris Dec 7 '12 at 12:20
3

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:

<?php
   $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;?>
{/exp:channel:entries}
1
  • That's a better way than the one I used, and pretty obvious too. Yet I could not think of it myself. – Andy Harris Dec 5 '12 at 20:13
0

I did this in the end like so:

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

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

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, privacy policy and cookie policy

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