Custom Fields Inside Joomla Modules

Usually in article modules like:

  • Articles – Categories
  • Articles – Category
  • Articles – Latest
  • Articles – Most Read
  • Articles – Newsflash
  • Articles – Related

but also on 3rd party modules that show articles, we want to include Custom fields.
Some of them can show the Custom Fields but others need to be hard-coded.
So you can do that by following these 2 steps:

  1. Add this code at the top before the close ( ?> ) of the “defined( ‘_JEXEC’ )or die;”
use Joomla\ Component\ Fields\ Administrator\ Helper\ FieldsHelper

2. Add this code where you want to display the Custom Fields

    <?php
      $customFields = FieldsHelper::getFields( 'com_content.article', $item, true );
      foreach ( $customFields as $customField ) {
      $customFields[ $customField->name ] = $customField;
      }
    ?> 
    <!-- Label of the field -->
    <p><?php echo $customFields['name-of-the-cf']->label; ?></p>
    <!-- Value of the field -->
	  <p><?php echo $customFields['name-of-the-cf']->value; ?></p>

If we want the code to check if the CF exists and then return the label/value here we are :

<?php
      $customFields = FieldsHelper::getFields( 'com_content.article', $item, true );
      foreach ( $customFields as $customField ) {
      $customFields[ $customField->name ] = $customField;
      }
 ?> 
 <?php
        if (!empty($customFields['name-of-the-cf']->label)) : ?>
        <span class="field-value"><?= $customFields['name-of-the-cf']->label; ?></span>
 <?php endif; ?>
 <?php
        if (!empty($customFields['name-of-the-cf']->value)) : ?>
        <span class="field-value"><?= $customFields['name-of-the-cf']->value; ?></span>
 <?php endif; ?>

It works on both, Joomla 3 and Joomla 4.

Scroll to Top