Loops Examples

Need a little help with our code? this code-snippets might be it.

Simple loop through categories:

Used mostly on the index page

{% for category in roots_categories %}
    <a ... href="{{category.url}}">
      <span>{{category.name}}</span>
    </a>
{% endfor %}

Looping through subcategories:

also listing the questions

 {% for subcategory in subcategories %}
            ...
         <a href="{{ subcategory.url }}">{{ subcategory.name }}</a>
            ...
        <ul ...>
              {% for question in subcategory.published_questions | limit:5 %}
                <li>
                      <a href="{{ question.url }}">{{ question.name }}</a>
                </li>
              {% endfor %}
        </ul>

    {% if subcategory.questions.size > 5 %}
      <a href="{{subcategory.show_all_questions_link}}" ... >See all {{subcategory.questions.size}} topics</a>
    {% endif %}
  {% endfor %}

Adding personalized Category/Subcategory text

Inside the category loop just test with a if condition to make it appear a subtext related to the category

{% for subcategory in subcategories %}
            ...
         <a href="{{ subcategory.url }}">{{ subcategory.name }}</a>
{% if subcategory.name == 'Customization'%}
<p>Subtext related to the category</p>
{%else%}
<p>Subtext related to the category</p>
{%endif%}

        <ul ...>

Just a parenthesis here the else condition is not required, and you can also use other conditions to cover the realm of possibilities using

{%elsif  subcategory.name == 'Customization'%}
Was this article helpful?

Can't find what you're looking for?

Contact Support