0

If I do print_r($this->item->jcfields) I see the array.

But when I try to call it in the usual way nothing aooears.

Does anyone know how to achieve this? They don’t appear in Params obviously. But if I can print them then surely I can echo them.

I really want to call separate fields in separate places.

2

At the bottom of the documentation https://docs.joomla.org/J3.x:Adding_custom_fields/Overrides it provides the technique for accessing individual fields within an override. So for a field called 'example':

<?php
// File location: /templates/templatename/html/com_content/category/blog_item.php

// loop first to make the fields easy to reference via the field name as a key
foreach($this->item->jcfields as $jcfield){
    $this->item->arbitrary_property_name[$jcfield->name] = $jcfield;
}

echo $this->item->introtext; 

echo $this->item->arbitrary_property_name['example']->label; 
echo $this->item->arbitrary_property_name['example']->value; 
?>

Also, what do you mean by the 'usual way'? There's like 25+ ways of getting custom fields.

  • Thank you. I had tried this but I could not get it working. And now when I copy the code I see it working but I only see the label and not the value. The fields are also showing below the content at the moment so I can see a value exists. Although oddly they are in a <p> and not a <dl>. I must have done something somewhere and forgotten it. In the above you are saying arbitrary_property_name could be anything? – Eoin Jan 4 '18 at 10:32
  • I meant... my usual way. Which is <?php echo $this->item->jcfields[3]->value; ?>. That will work in an article but not a blog apparently. Actually this will also show the label but not the value. Curiouser and curiouser. I printed the params but there is no value in there either. I used <?php echo "<pre>" . print_r($this->item->jcfields, true) . "</pre>"; ?> – Eoin Jan 4 '18 at 10:46
  • Arggghh so much wasted time. The fields were pasted as content into the main copy and the fields were then not filled in. How stupid do I have to be! It must have been working previously because of a different category. – Eoin Jan 4 '18 at 10:51
  • 1
    lol. glad you found your answer, tho. – jamesgarrett Jan 4 '18 at 23:02
1

Thanks for posting not only the script but also the file location ;).

For the article view (single article) use the path above: // File location: /templates/templatename/html/com_content/category/blog_item.php

For the category view (blog) use this path: // File location: /templates/templatename/html/com_content/article/default.php

You can also style the output directly in the files a bit, for example:

<div style="color:#333333; font-weight: 600; font-size:16px;"> 
        <?php
        // File location: /templates/templatename/html/com_content/category/blog_item.php

        // loop first to make the fields easy to reference via the field name as a key
        foreach($this->item->jcfields as $jcfield){
            $this->item->arbitrary_property_name[$jcfield->name] = $jcfield;
        }

        echo $this->item->arbitrary_property_name['example']->label; 
        echo $this->item->arbitrary_property_name['example']->value; 
        ?>
    </div>

hope, that helps..

  • Great first post. Welcome.to Stack. – Eoin Nov 6 '18 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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