1

Here's the breakdown:

» structure with two categories. Each category has +10 entries.

» Template is structure / index.html and _entryview.html

I'd like to show two links and a description of both categories on the /structure/ URL.

However...

On the /structure/index/ page, I'd like to show the entries for each category — all using the same index template.

So far I can pull the {category.title}s and the {entry.title} ... but it's the same on both pages (structure/ and structure/category)

{% set categories = craft.categories.group('fellowsCat') %}

 <ul>
  {% nav category in craft.categories
    .group('fellowsCat')
    .limit(11)
    %}
    <li>
      <a href="{{ category.url }}">{{ category.title }}</a>
      {% ifchildren %}
        <ul>
          {% children %}
        </ul>
      {% endifchildren %}
    </li>
  {% endnav %}
</ul> 

<ul>

{% set entries = craft.entries({
  section:  'fellows',
  relatedTo:  craft.categories('fellowsCat'),
})
%}


{# ___________________________
this is where things fall apart. 
If I'm on /structure/ I don't want to see entries. 
If I'm on /structure/category, I do. 
_____________________________
#}


{% if craft.categories is not null %}

  {% for entry in entries %}
    <li>  <a href="{{ entry.url }}">{{ entry.title }}</a></li>
  {% endfor %}
  </ul>

{% else %}
nothing to see
{% endif %}

I suspect there's something with the {% if craft.categories is not null %} line that would do the job, but I can't figure it out.

1

Figured it out.

    {% set categories = craft.categories.group('fellowsCat') %}

 <ul>
  {% nav category in craft.categories
    .group('fellowsCat')
    %}
    <li>
      <a href="{{ category.url }}">{{ category.title }}</a>
      {% ifchildren %}
        <ul>
          {% children %}
        </ul>
      {% endifchildren %}
    </li>
  {% endnav %}
</ul> 

<ul>


{# --- this is the key line here ---#}
{% if category is defined %}

{% set entries = craft.entries({
  relatedTo:  category,

})
%}

{% if craft.entries.relatedTo(category) %} 


      {% for entry in entries %}
        <li>  <a href="{{ entry.url }}">{{ entry.title }}</a></li>
      {% endfor %}
      </ul>

     {% else %} 
    nothing to see
     {% endif %} 
     {% endif %}
  • Just a side note {% if craft.entries.relatedTo(category) %} will always become true, no matter what. There is no legit way to go to the else part – Robin Schambach Apr 19 '18 at 20:36

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.