4

I'm trying to get the custom fields value of a category in a blog view. I have try the article item method but on the child item (supposed to be the current category item in the loop) :

JLoader::register('FieldsHelper', JPATH_ADMINISTRATOR . '/components/com_fields/helpers/fields.php');

then in the loop

<?php foreach ($this->children[$this->category->id] as $id => $child) : ?>
<?php foreach($child->jcfields as $jcfield)
 {
      $child->jcFields[$jcfield->name] = $jcfield;
 }

and then

<?php echo "test:".$child->jcFields['titre-discipline']->label; ?>

but i get nothing, no error, nothing. One the category actually get value in. THe custom show correctly in the category item view but not in this blog view.

Thanks for your help

1
  • 1
    Is error reporting turned on? I would recommend dropping <?php echo '<pre>',print_r($child,1),'</pre>'; ?> into your script. It will print out the multidimensional array containing the article data in a hierarchical / human readable format. Once you can see the content of your array it is easier to debug. Oct 5, 2018 at 18:52

1 Answer 1

0

Load the fields with FieldsHelper::getFields(). This allows to access each field individually by its name.

<?php foreach(FieldsHelper::getFields('com_content.categories', $child) as $field) : ?>
    <?php $child->jcFields[$field->name] = $field; ?>
<?php endforeach; ?>
<?php echo "test:".$child->jcFields['titre-discipline']->label; ?>

If you just want to render the fields using the standard layout, use FieldsHelper::render() instead:

<?php echo FieldsHelper::render(
    'com_content.categories',
    'fields.render',
    array('context' => 'com_content.categories', 'item' => $child, 'fields' => FieldsHelper::getFields('com_content.categories', $child))
); ?>
4
  • Thanks for your help but this I just get the informations about which fields is affected to the categories but not the custom fields themselves. O r didn't use it correctly. Did you have try your self on a sub cat in a blog view ?
    – Semaphore
    Oct 30, 2018 at 13:22
  • First snippet allows to display individual field properties of individual fields. Now it would display the label of a field named titre-discipline. To display a different field, you would change titre-discipline to the name of another field. And to display a different property (e.g. value), you would change label to value.
    – Sharky
    Oct 30, 2018 at 13:47
  • In case you want to render all fields normally using the standard layout, see the updated answer.
    – Sharky
    Oct 30, 2018 at 13:53
  • Hi, Ok that's render all the custom fields for each sub-cat ! Sorry to ask you so much but what is the best code to get thse custom fields in a usable object like $child->jcFields['titre-discipline']->value for example ? I have the logic but can't achieve it :-/ Even after reading api.joomla.fr/joomla3/d1/df7/… Render to do not send back an arry (because it's a rendering function I get that).
    – Semaphore
    Oct 31, 2018 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.