display random child
Posted: Thu May 05, 2011 9:58 am
Hello,
I'd like to share a code snippet to grab/display a random child with cgsimple:
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:
In the example above I'll write the content of the field 'field' into variable 'myvar'. And print it:
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.
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}
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')}
Code: Select all
<span> {eval var=$myvar} </span>