Page 1 of 1

Custom field on category-blog override

Posted: Mon Aug 31, 2020 1:25 pm
by Mr. Wimpy
Hey, hey,

I want to add the value of a custom field to a category-blog overide.
I have done this several times on an article overide, but on a blog overide it doesn't work.

I use this code at the top of the override

Code: Select all

<?php $myfield = $this->item->jcfields;
foreach($myfield as $field) {
    $myfield[$field->name] = $field;
} ?>
and add this code to display the value at the spot I want it

Code: Select all

<?php echo $myfield['name-of-field']->rawvalue; ?>
Yes, this code does work perfectly on article overrides
Yes, it is a custom field for category.
Yes, the field is published etc, it displays fine on the location set at Automatic display.

What am I missing?
Tried both on J3 and J4.

Thank you.

Re: Custom field on category-blog override

Posted: Tue Sep 01, 2020 8:48 am
by pe7er
Have you created a template override of
/components/com_content/views/category/tmpl/blog.php or of
/components/com_content/views/category/tmpl/blog_item.php ?

I have not tested this but I would try changing the template override /templates/your-template/html/com_content/category/blog.php

Code: Select all

$this->item = &$item;
echo $this->loadTemplate('item');

Code: Select all

$this->item = &$item;
$myfield = $this->item->jcfields;
foreach($myfield as $field) {
    $this->item->myfield[$field->name] = $field;
echo $this->loadTemplate('item');
and then in the override /templates/your-template/html/com_content/category/blog_item.php I would add

Code: Select all

<?php echo $this->item->myfield['name-of-field']->rawvalue; ?>
You might want to do

Code: Select all

echo "<pre>";
print_r($this->item->myfield); ?>
echo "</pre>";
first to see if your custom fields are actually loaded correctly...

Re: Custom field on category-blog override

Posted: Tue Sep 01, 2020 1:58 pm
by Mr. Wimpy
Hello Peter,

I have not tried your code, because that looks it's for blog_item.php.
My snippets of code do display the data from Article custom fields when I add it to blog_item.php.

I am talking about blog.php.

I have created the override and it is loaded.
I have created the custom fields for category.
I have entered data in those fields for the category.
The data from those fields are displayed fine on the position Before Display (set at Automatic display)

But, the data Will not display on the location where I added my second snippet of code.

PS. I also tried the code snippets from the docs: ... /Overrides (at the bottom)
No luck...

Re: Custom field on category-blog override

Posted: Tue Sep 01, 2020 2:11 pm
by pe7er
Joomla's category blog view uses blog.php to load smaller subviews: (_children, _item & _links).

For each of the articles that are retrieved in the model,
the blog.php view will load
the blog_item.php subview with the code

Code: Select all

echo $this->loadTemplate('item');
In blog.php you can retrieve the custom fields and add them to the $this->item object and
in blog_item.php you can display the parts of the object (article title, etc, and the custom fields that you have added to the article).

Re: Custom field on category-blog override

Posted: Tue Sep 01, 2020 6:04 pm
by Mr. Wimpy
I feel we are talking about different things here...
Maybe I haven't explained myself clearly enough.

Let me post some images on what I mean.

When you go to Content > Fields you can create fields for Articles or Category.

The Category fields are added on the Category Edit page.

These fields are only displayed on the category page.

I would like to use the value of the category fields somewhere else on the category blog page.
On blog.php, not on blog_item.php.