1
{% set slug = craft.request.getParam('s') %}

{% if slug | length %}
    {% set category = craft.categories.slug(slug) %}
    {% set faqs = craft.entries.section('faq').order('title').limit(null) %}

    <h3>{{ category.title }}</h3>

    <ul class="no-bullet">
        {% for entry in faqs.relatedTo(category) %}
            {% if entry.level >1 %} 
                <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
            {% endif %}  
        {% endfor %}
    </ul>
{% else %}
    <h3> No matching results found </h3>
{% endif %} 

In the code above, I cannot get {{ category.title }} to display. It is empty. If I put {{ category.slug }} instead, it shows the correct value. The other items in unordered list are displaying perfectly. I am confused as to why the title is not showing

1
  • If you simplify it to {% set slug = craft.request.getParam('s') %}{% set category = craft.categories.slug(slug) %}{{ category.title }} does it work? Is this a multi-locale setup?
    – Brad Bell
    Apr 17 '17 at 18:44
1

Try changing this:

{% set category = craft.categories.slug(slug) %}

to

{% set category = craft.categories.slug(slug).first() %}
2
  • Brad, you nailed it. I still don't understand why you have to explicitly mention .first() since there is only one object that is returned.
    – hvs
    Apr 17 '17 at 19:06
  • Slugs are not unique, ids are, so there is a chance you could have more than one category being returned. Don't quote me on that, but just remember to use first()
    – Brad
    Apr 17 '17 at 20:42

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.