Custom field on category-blog override

Everything to do with Joomla! 3.x templates and templating.
Post Reply
Mr. Wimpy
Joomla! Explorer
Joomla! Explorer
Posts: 331
Joined: Fri Dec 02, 2005 10:46 am
Location: The Netherlands

Custom field on category-blog override

Post by Mr. Wimpy » Mon Aug 31, 2020 1:25 pm

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.
Wim

User avatar
pe7er
Joomla! Master
Joomla! Master
Posts: 22925
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, Netherlands
Contact:

Re: Custom field on category-blog override

Post by pe7er » Tue Sep 01, 2020 8:48 am

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');
to

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

<?php  
echo "<pre>";
print_r($this->item->myfield); ?>
echo "</pre>";
?>
first to see if your custom fields are actually loaded correctly...
Kind Regards,
Peter Martin, Global Moderator
https://db8.nl - Joomla specialist, Nijmegen, Nederland
The best website: https://the-best-website.com

Mr. Wimpy
Joomla! Explorer
Joomla! Explorer
Posts: 331
Joined: Fri Dec 02, 2005 10:46 am
Location: The Netherlands

Re: Custom field on category-blog override

Post by Mr. Wimpy » Tue Sep 01, 2020 1:58 pm

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:
https://docs.joomla.org/J3.x:Adding_cus ... /Overrides (at the bottom)
No luck...

User avatar
pe7er
Joomla! Master
Joomla! Master
Posts: 22925
Joined: Thu Aug 18, 2005 8:55 pm
Location: Nijmegen, Netherlands
Contact:

Re: Custom field on category-blog override

Post by pe7er » Tue Sep 01, 2020 2:11 pm

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).
Kind Regards,
Peter Martin, Global Moderator
https://db8.nl - Joomla specialist, Nijmegen, Nederland
The best website: https://the-best-website.com

Mr. Wimpy
Joomla! Explorer
Joomla! Explorer
Posts: 331
Joined: Fri Dec 02, 2005 10:46 am
Location: The Netherlands

Re: Custom field on category-blog override

Post by Mr. Wimpy » Tue Sep 01, 2020 6:04 pm

:pop
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.
catfield1.jpg

The Category fields are added on the Category Edit page.
catfield2.jpg

These fields are only displayed on the category page.
catfield3.jpg

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.
You do not have the required permissions to view the files attached to this post.


Post Reply

Return to “Templates for Joomla! 3.x”