Find and print categories in twig file

I have edited the default home page (/user/pages/01.home) and changed the page file into “Modular”.

Under the home page, I’ve created a new modular page called “Categories” (/user/pages/01.home/_categories).

Under the categories page, I’ve added another modular page, where each modular page has a taxonomy like “Term 1”, “Term 2”, “Term 3”, “Term 4” and so on.

I’ve created a *.html.twig to print those content.

Now, how can I find the categories used and group them based from their taxonomy terms? Also, I would like to add the category itself as a title on each group.

This is what I’ve got so far (which I found in the documentation), and I’ve wanted to find the categories dynamically instead of typing them one by one just to find and print the taxonomy associated with it.

{% for post in taxonomy.findTaxonomy({'category':['Term 1']}) %}
  {{ post.title }}
  {{ post.content }}
{% endfor %}

{% for post in taxonomy.findTaxonomy({'category':['Term 2']}) %}
  {{ post.title }}
  {{ post.content }} 
{% endfor %}

{% for post in taxonomy.findTaxonomy({'category':['Term 3']}) %}
  {{ post.title }}
  {{ post.content }}
{% endfor %}

{% for post in taxonomy.findTaxonomy({'category':['Term 4']}) %}
  {{ post.title }}
  {{ post.content }}
{% endfor %}
---

You mean if you had a page with frontmatter like:

title: My Page 1
taxonomy:
   category: ['Term 1']

and another page with:

title: My Page 2
taxonomy:
   category: ['Term 2']

This Twig template could be shared between them:

{% for post in taxonomy.findTaxonomy({'category': page.taxonomy.category}) %} 
  {{ post.title }}
  {{ post.content }}
{% endfor %}

Hopefully i’m not misunderstanding your question…

Thank you for your quick response rhukster. Yep, that is what in my frontmatter. When I’ve copied and pasted the code you’ve provided, there are no content printed. The output is “null” when I tried to debug the “page.taxonomy.category”.

I forgot to mention that my template for the categories is located in my template partials and just included that file in my base.html.twig.

Maybe you can zip up what you have and put it on dropbox for me. PM me the link and I can see what your actually trying to do :slight_smile:

Hi rhukster, kindly check your inbox. Thank you.

This should give you an idea… try this

<div class="menu-items">
	<div class="container">
	     {% set terms = [] %}
	     {% for post in page.find('/home/_03menu').children %}
	       {% for term in post.taxonomy.category %}
	         {% if term not in terms %}
	           {% set terms = terms|merge([term]) %}
	         {% endif %}
	       {% endfor %}   
	     {% endfor %}
	    
	    {% for term in terms %}
	      <div class="col-lg-3">
	        <h2> {{ term }} </h2>
	        {% for p in taxonomy.findTaxonomy({'category' : term}) %}
	          <div class="item row">
	            <h3> {{ p.header.title }} </h3>
	            <p> {{ p.content }} </p>
	          </div>
	        {% endfor %} 
	      </div>
	    {% endfor %}
		
	</div>
</div>
---

Thank you danreb. The code works well :slight_smile:

I saw danreb beat me to it, nice.