display random child

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
dwave
Forum Members
Forum Members
Posts: 39
Joined: Mon Aug 13, 2007 11:15 am
Location: Israel

display random child

Post by dwave »

Hello,
I'd like to share a code snippet to grab/display a random child with cgsimple:

Code: Select all

{$cgsimple->get_children('parent_alias',0,'children')}
{assign var=randomindex value=$children|@array_rand} 
The parameter 'parent_alias' is just the page alias of the parent whose children you want. The variable $randomindex will contain a random index from the array $children from the PHP function 'array_rand' as modifier. It's as simple as that (tested with 1.9.4). No awkward loops are needed!

Then you can access the child's content with the usual cgsimple methods, e.g:

Code: Select all

{$cgsimple->get_page_content($children.$randomindex.alias,'field', 'myvar')}
In the example above I'll write the content of the field 'field' into variable 'myvar'. And print it:

Code: Select all

<span> {eval var=$myvar} </span>
I hope this helps. It took me a while to figure it out but with a bit of Smarty magic CMS Made Simple can be used for quite powerful, flexible (and simple) content customizations. Thanks again for the developers for their ongoing effort.
mesaredo
Forum Members
Forum Members
Posts: 27
Joined: Mon Jul 27, 2009 1:23 pm

Re: display random child [SOLVED]

Post by mesaredo »

I dwave:

Could you indicate where you place each code (template, page, etc..) Thank you!
dwave
Forum Members
Forum Members
Posts: 39
Joined: Mon Aug 13, 2007 11:15 am
Location: Israel

Re: display random child

Post by dwave »

Mesaredo,
this Smarty snippet works in templates as well as in Global Content Blocks. Make sure that the module CGSimpleSmarty is installed, so that the class cgsimple is available.

Another example to display a random title from one of the child pages of the page "parent":

Code: Select all

{$cgsimple->get_children('parent',0,'children')}
{assign var=randomindex value=$children|@array_rand} 
{$cgsimple->get_page_title($children.$randomindex.alias, 'title')}
{eval var=$title}
Post Reply

Return to “Tips and Tricks”