[tips] Custom fields for non-developer

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
?>

Alexandre ELISÉ

#Joomla technical problem solver. Focusing on secure coding and defensive cybersecurity #appsec #blueteam.#coderparlerpartager Now working @aproximito.

Get the newsletter

Super Joomlers! Get the most out of Joomla!
We hate spam as much as you do and we will not send you email without your prior consent. You can unsubscribe / resubscribe any time you want.