Compatible with Joomla 5 and Joomla 6 (PHP 8.x). This tutorial shows how to display a single Custom Field or multiple Custom Fields inside a Joomla article template override, using their field names instead of numeric array positions.
1. Create the Article Template Override
Go to System → Site Templates → Your Template → Create Overrides and select Components → com_content → article. Joomla creates the following file:
templates/YOUR_TEMPLATE/html/com_content/article/default.php
You can also add the examples to an existing article layout override. Insert the PHP code in the override file on the server, not into the Joomla article itself.
2. Build an Array Indexed by Field Name
Near the beginning of your article override, after defined('_JEXEC') or die; and any existing initialization code, add:
<?php
$fields = [];
if (!empty($this->item->jcfields) && is_array($this->item->jcfields)) {
foreach ($this->item->jcfields as $field) {
$fields[$field->name] = $field;
}
}
?>
This makes each field accessible by its unique Name (for example, tour-dates). It avoids relying on the order of the jcfields array.
Instead of:
<?php echo $this->item->jcfields[4]->value; ?>
Use:
<?php echo $fields['tour-dates']->value ?? ''; ?>
3. Display One Custom Field
Assume that the article has a Text Custom Field whose Name is tour-dates. To show its value:
<?php echo $fields['tour-dates']->value ?? ''; ?>
For a plain-text field, it is better to check for an empty value and escape the text before printing it:
<?php if (!empty($fields['tour-dates']->value)) : ?>
<div class="tour-dates">
<?php echo htmlspecialchars(
(string) $fields['tour-dates']->value,
ENT_QUOTES,
'UTF-8'
); ?>
</div>
<?php endif; ?>
4. Display Multiple Fields
Once the $fields array is built, you can use it throughout the same override. Example with tour-dates, tour-location and tour-price:
<div class="tour-details">
<?php foreach (['tour-dates', 'tour-location', 'tour-price'] as $name) : ?>
<?php if (!empty($fields[$name]->value)) : ?>
<div class="mb-2">
<strong><?php echo htmlspecialchars(
(string) $fields[$name]->title,
ENT_QUOTES,
'UTF-8'
); ?>:</strong>
<?php echo htmlspecialchars(
(string) $fields[$name]->value,
ENT_QUOTES,
'UTF-8'
); ?>
</div>
<?php endif; ?>
<?php endforeach; ?>
</div>
The example uses the Joomla field title as the visible caption and Bootstrap 5 spacing classes. Replace the names with your own field names.
5. Display a Field Title and Value
<?php if (!empty($fields['tour-dates']->value)) : ?>
<div class="tour-dates">
<strong><?php echo htmlspecialchars(
(string) $fields['tour-dates']->title,
ENT_QUOTES,
'UTF-8'
); ?>:</strong>
<?php echo htmlspecialchars(
(string) $fields['tour-dates']->value,
ENT_QUOTES,
'UTF-8'
); ?>
</div>
<?php endif; ?>
6. Understanding value and rawvalue
| Property | Description |
|---|---|
name |
Unique machine-readable field name |
title |
Field title |
value |
Prepared field value; presentation depends on the field type |
rawvalue |
Original stored field value |
type |
Custom Field type |
For example:
<?php echo $fields['tour-dates']->rawvalue ?? ''; ?>
Note: These plain-text examples assume a scalar text value. Some field types (such as media, editor, list with multiple selections, or third-party fields) may produce HTML or arrays and need type-specific rendering. Do not blindly pass arrays into htmlspecialchars(). For Joomla-managed field presentation, consider the core field layout/renderer instead.
7. Display All Available Plain-Text Fields
To loop through all fields in the prepared array:
<?php if (!empty($fields)) : ?>
<div class="article-custom-fields">
<?php foreach ($fields as $field) : ?>
<?php if (is_scalar($field->value) && (string) $field->value !== '') : ?>
<div class="custom-field mb-2">
<strong><?php echo htmlspecialchars(
(string) $field->title,
ENT_QUOTES,
'UTF-8'
); ?>:</strong>
<?php echo htmlspecialchars(
(string) $field->value,
ENT_QUOTES,
'UTF-8'
); ?>
</div>
<?php endif; ?>
<?php endforeach; ?>
</div>
<?php endif; ?>
This loop is for plain-text output. For automatic Joomla field rendering (including field-specific HTML), use the standard Joomla Fields layout as appropriate.
8. Joomla Settings and Troubleshooting
- Use each field's exact Name, not its numeric position.
- To prevent duplicate output, set the field's Automatic Display option to Do not automatically display when displaying it manually.
- Check field availability, access and assigned categories if a field is missing.
- Use
$this->itemfor the article view; category blog layouts commonly expose an$itemvariable instead. Check thatjcfieldsis present on that object. - Keep your site's regular Joomla article view layout intact; insert only the relevant snippets into the existing override.
9. Minimal Reusable Example
This code combines the initialization and a single field in one snippet, suitable for an existing article override:
<?php
$fields = [];
if (!empty($this->item->jcfields) && is_array($this->item->jcfields)) {
foreach ($this->item->jcfields as $field) {
$fields[$field->name] = $field;
}
}
?>
<?php if (!empty($fields['tour-dates']->value)) : ?>
<div class="tour-dates mb-3">
<strong>Tour Dates:</strong>
<?php echo htmlspecialchars(
(string) $fields['tour-dates']->value,
ENT_QUOTES,
'UTF-8'
); ?>
</div>
<?php endif; ?>
Conclusion
Creating a field-name-indexed array once in a Joomla article override is a reliable approach when you need precise placement and custom markup. It is easier to maintain than numeric indexes and works well for a single field or several fields. Always choose the appropriate output handling for each field type.
Important: The PHP snippets above are documentation examples. They are displayed in the article as text; actual executable PHP belongs in your template override files.