1

I have a Video section with a field called videoCategory. I'm trying to pull in the first category on the _entry.html template.

I'm pretty much following the the last example in the documentation at: http://buildwithcraft.com/docs/categories-fields

...with this code:

 {% set entryCategory = entry.videoCategory.first() %}
 {% if entryCategory %}
   <h1>{% for category in entryCategory %}<a href="">{{ category.title }}</a> {% endfor %}</h1>
 {% endif %}

But I can't get around this error message:

Impossible to access an attribute ("title") on a string variable ("19")

What is it that I'm missing?

4

entryCategory is already a single category because of "first()". So no need for the for loop. Just {{ entryCategory.title }} should do the trick here.

{% set entryCategory = entry.videoCategory.first() %}
{% if entryCategory %}
    <h1><a href="">{{ entryCategory.title }}</a></h1>
{% endif %}
  • Too simple. Only my first week with Craft, so I'm full of these little questions. Thanks jkenters! – John Johnson May 11 '15 at 17:43

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.