PHP
Joomla! 6
Joomla! 5
Joomla! 3 (Legacy)
Template Override
Advanced
Render Custom Fields Grouped by Field Group Conditionally
Groups all populated Custom Fields by group_title and renders them conditionally based on the active menu page class.
templates/seu_template/html/com_content/article/default.php
<?php
declare(strict_types=1);
use Joomla\CMS\Factory;
$active = Factory::getApplication()->getMenu()->getActive();
$rawClasses = $active ? trim((string) $active->getParams()->get('pageclass_sfx', '')) : '';
$pageClasses = $rawClasses !== '' ? (preg_split('/\s+/', $rawClasses) ?: []) : [];
if (\in_array('perfis', $pageClasses, true) && !empty($this->item->jcfields)) :
$grouped = [];
foreach ($this->item->jcfields as $field) {
if (trim((string) $field->value) === '') {
continue;
}
$groupName = (string) ($field->group_title ?: 'Geral');
$grouped[$groupName][] = $field;
}
?>
<aside class="col-md-3">
<?php foreach ($grouped as $groupTitle => $fields) : ?>
<h3><?php echo htmlspecialchars($groupTitle, ENT_QUOTES, 'UTF-8'); ?></h3>
<ul class="square-checkmark">
<?php foreach ($fields as $field) : ?>
<li>
<strong><?php echo htmlspecialchars((string) $field->label, ENT_QUOTES, 'UTF-8'); ?>:</strong>
<?php echo $field->value; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
</aside>
<?php endif; ?>
Pre-groups non-empty fields per group_title so HTML lists are always balanced and clean.
templates/seu_template/html/com_content/article/default.php
<?php
$yourParameter = explode(' ', JFactory::getApplication()->getMenu()->getActive()->params->get('pageclass_sfx'));
?>
<?php if (in_array('perfis', $yourParameter)) : ?>
<div class="col-md-3 span3">
<?php JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php'); ?>
<?php
$cf = 0;
$group = array();
foreach ($this->item->jcfields as $kf => $field) :
$count = $cf++;
$group[$count] = $this->item->jcfields[$kf]->group_title;
$prevGroup = $count > 0 ? $group[$count - 1] : null;
if (($prevGroup != $group[$count]) && ($field->value)) :
if ($count > 0) {
echo '</ul>';
}
echo '<h3>' . $group[$count] . '</h3>';
echo '<ul class="square-checkmark">';
endif;
if ($field->value) {
echo '<li>' . $field->label . ': ' . $field->value . '</li>';
}
endforeach;
?>
</ul>
</div>
<?php endif; ?>
Original Joomla 3.7+ override example filtered by pageclass_sfx.
Technical explanation and best practices
Built for spec sheets, team profiles or structured directory entries using multiple Custom Field groups. Disable automatic display on each field so your override controls the exact markup.
The Joomla 5 and 6 version pre-groups populated fields into an associative array by group_title, guaranteeing valid balanced <ul> markup.