Jump to content

Random quote, limit 1 from repeater


Peter Knight
 Share

Recommended Posts

Hi guys

A few pages on my site are required to output a random quote from a bunch of 5 which are associated with that page.

IE 

About has 5 quotes
Home has 5 quotes
Company has 5 quotes

Normally I'd just make a few child pages for each parent and give it it's own template. Using a selector to do that would be easy.

However, I've decided to go down the route of adding each pages 5 quotes to a repeater field.

I can output ALL the pages quotes using this...

<?php
foreach($page->one_liner_group as $one) {
echo "{$one->one_liner}<p>"; 
}?>

but I'm not so sure where to add (limit=1) and getRandom when using repeaters within a page.

Thanks

 

Link to comment
Share on other sites

It should be even more straight forward:

<p><?= $page->one_liner_group->getRandom()->one_liner ?></p>

The value of a repeater field is a RepeaterPageArray, which is just a slightly extended PageArray.

Edit: @Soma beat me by a few seconds ?

  • Like 2
Link to comment
Share on other sites

Thank you!

So straightforward when you see how.

I got up at 5AM this morning to work on this project and thought I'd get a good 12 hours at it. Instead, I had computer and internet problems all day. At least I can wake up tomorrow AM (not at 5AM though) knowing I have this fixed ?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...