x
 
Create New Page
 View Previous Changes    ( Last updated by Lisa Wess )

Display random field

Question:

How can I display a single weblog field chosen randomly from a set of fields?

Answer:

By using PHP (parsed on OUTPUT), this can be done. The example below illustrates the general approach using three fields. It is also assumed that you do not want to display an empty field, so fields devoid of content are eliminated from the set of possibilities.

{exp:weblog:entries weblog="weblog1"}

<?php
$fields
= array(
0=>'{field_shortname1}',
1=>'{field_shortname2}',
2=>'{field_shortname3}' );

foreach(
$fields as $fieldkey => $field)
{
if (trim($field) == '')
{
unset( $fields[$fieldkey] );
}


} shuffle
($fields);
echo
$fields[0];
?>

{
/exp:weblog:entries}

Category:Entries Category:Tricks

Categories: