Snippet: Template logic to fetch random content object

Table of contents:

Example template

The following example shows how to fetch a random content objeect, an image in this example.

{def $nrimages=fetch( 'content', 'tree_count', hash( 'parent_node_id', 2, 'class_filter_type', 'include', 'class_filter_array', array('image') ) )}
{def $randomimage=fetch( 'content', 'tree', hash( 'parent_node_id', 2, 'class_filter_type', 'include', 'class_filter_array', array('image'), 'offset', rand(0, dec($nrimages)), 'limit', 1 ) )}
{content_view_gui content_object=$randomimage[0] view=embed}

Reference

  • Forum: N/A