Using Category Custom Fields
Once you become comfortable adding category custom fields anywhere that you need them, you will discover a whole new world of possibilities and be able to provide more value to each of your projects.
Category Custom Fields
Including category custom fields anywhere in your template that uses the 'Category Object' programmatically can be accomplished using the following steps:
-
- Add the following line to the top of the php file you want to the custom field to display on:
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
- Depending on where the category object is being called, anywhere in the top php code would work. Live examples of category custom fields in use on this site can be found at 'templates/mytemplate/includes/pagecontent.php' :
$CatFields = FieldsHelper::getFields('com_content.categories', $category, true);
foreach($CatFields as $Catfield)
{
$CatFields[$Catfield->name] = $Catfield;
} - Add the custom fields you wish to include after the code in Step 2:
$mycategorycustomfield = $CatFields['mycategorycustomfield-alias']->rawvalue;
- Add the following line to the top of the php file you want to the custom field to display on:
Breaking down the following line from Step 2:$CatFields = FieldsHelper::getFields('com_content.categories', $category, true);
- 'com_content.categories': This tells the code to look for category custom fields related to the 'Category Object'
- Category Object - $category
$CatFields = FieldsHelper::getFields('com_content.categories', $category, true);
- 'true':indicates whether to display all the field data or (false) just the raw data - changing to false will shave a little time off the page load (especially if you are using a lot of custom fields)
Breaking down the following line from Step 3:$mycategorycustomfield = $CatFields['mycategorycustomfield-alias']->rawvalue;
- '$mycategorycustomfield': This can be whatever unique name you prefer to use to name your custom field variable (as long as it starts with '$')
- 'mycategorycustomfield-alias': Alias name of the category custom field.
- 'rawvalue':indicates we are using the "raw" value of the custom field as opposed to the
$mycategorycustomfield = $CatFields['mycategorycustomfield-alias']->value;
That displays the default layout display of the custom field.
Finding the 'Category Object'
Often times, integrating multiple areas together involves some old-fashioned detective work to figure out how to grab the desired Category Object. The following steps will help show you how to find the 'Category Object':
- At the top of the page (after the last line that starts with 'use') add:
use Joomla\CMS\Categories\Categories;
- Article Category Page - $this->category->id (although in this case, we already have the 'Category Object' which is '$this->category')
- Article Page - $this->item->catid
- Article Module - $item->catid (inside the 'foreach statement')
Once you have figured out what the category id is for the category you wish to add custom fields from, we will then use the category id to create the 'Category Object'. To achieve this, add the following lines $catid = $this->category->id;
(or whatever the 'Category Object' is)$category = Categories::getInstance('Content')->get($catid);
above$CatFields = FieldsHelper::getFields('com_content.categories', $category, false);
Category Object Cheat Sheet
- Article Category Page - $this->category
$CatFields = FieldsHelper::getFields('com_content.categories', $this->category, true);
- Article Page - $category
$CatFields = FieldsHelper::getFields('com_content.categories', $category, true);
- Article Module - $category (inside the 'foreach statement')
$CatFields = FieldsHelper::getFields('com_content.categories', $category, true);
Category Custom Field Example
The following example will add add category custom fields to an 'Articles - Category' module using a layout override:
- Create a 'Text Type' category custom field titled 'Category Tester 1' with the alias 'category-tester-1' and add some text to the field for the selected article that will display in the module.
- Create a new layout override for the module:
- 'System > Site Templates > [Your Template] > Create Overrides'
- Under 'Modules' click 'mod_articles_category'
- Select the 'Editor' tab on the same screen then click the html folder
- Click the folder 'mod_articles_category'
- Click the 'default.php' file then click 'Rename File' at the top of the page and rename to 'categorytester.php'
- Repeat for theĀ 'default_items.php' file renaming to 'categorytester_items.php'
- Open theĀ 'categorytester_items.php' file around line 16 add:
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
use Joomla\CMS\Categories\Categories; - Around line 18 replace:
<?php foreach ($items as $item) : ?>
With:<?php foreach ($items as $item) :
$catid = $item->catid;
$category = Categories::getInstance('Content')->get($catid);
$CatFields = FieldsHelper::getFields('com_content.categories', $category, false);
foreach ($CatFields as $Catfield)
{
$CatFields[$Catfield->name] = $Catfield;
}
$categorytester1 = $CatFields['category-tester-1']->rawvalue; ?> - Look for
<?php if ($item->displayDate) : ?>
(originally around line 47) and on an empty line above it add:<?php echo $categorytester1;?>
- In your 'Articles - Category' module, click the 'Advanced' tab and look for the 'Layout' dropdown field. Select the newly created layout titled 'CategoryTester' and then 'Save'.
Using echo
At the most basic level displaying items in PHP is as simple as a basic 'echo' statement such as <?php echo $tester1;?>
where the $tester1 variable represents $jcFields['tester-1']->rawvalue;
We could have also accomplished the same by calling the full variable like:<?php echo $jcFields['tester-1']->rawvalue;?>
If you have followed the steps and have managed to not get confused by the above instructions, you should now see whatever text you added to the 'Category Custom Field' in the place you added the <?php echo $categorytester1;?>
for each of your displayed articles in the module. 'Category Custom Fields' work best when you want to add additional fields to the category page itself or where you want to display additional images/icons or text on articles that live within those specific categories.