esedic
3/7/2017 - 2:31 PM

Get Joomla 3.7+ custom field in template

Get Joomla 3.7+ custom field in template

<?php
/**
 * This example works with template override on default Protostar template.
 * Overridden file is /html/com_content/article/default.php.
 * Custom field parameter "Automatic Display" is set to "No".
 */

// 1. iterate over all custom fields
foreach ($this->item->jcfields as $field)
  {
    echo 'Field title: ' . $field->title.'<br>';
    echo 'Field value: ' . $field->value;
  }

// 2. get single custom field
foreach ($this->item->jcfields as $field) {
	$jcfields[$field->name] = $field; // using field name for identifying field (potential identifiers: id, title, name, label, type, etc)
	$jcfields[$field->label] = $field;
}

$modelvalue = $jcfields['model']->value; // name of the field is "model", you can also use `rawvalue` instead of `value`
$modellabel = $jcfields['model']->label; 

echo $modellabel.': '.$modelvalue;

?>