[tips] Custom fields for non-developer

Hello super joomlers!

This time, I will show you a simple code snippet to copy paste in your template overrides of com_content (articles). For those of you who want to add custom fields in their favourite templates there are 2 snippets to add. Here they are.

One at the top of the file and the other where you want to show the custom field (This requires Joomla! 3.7.x minimum).
for example: In the file templates/example/html/com_content/article/defaut.php

<?php
defined('_JEXEC') or die();
//your template code...
// BEGIN: Code to add at the top of the file
JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');
$fields_by_name = \Joomla\Utilities\ArrayHelper::pivot($this->item->jcfields, 'name');
// END: Code to add at the top
?>
<?php
//...
//Your template code...
//...
// BEGIN: Code to add where you want to show the custom field by name
echo $fields_by_name['my-custom-field']->label
. ':' . $fields_by_name['my-custom-field']->value;
// END: Code to add where you want to show the custom field by name
?>

That's it!. Take care super joomlers!

Articles similaires