Skip to content

Instantly share code, notes, and snippets.

@mdcpepper
Created October 24, 2017 11:40
Show Gist options
  • Save mdcpepper/40113b6425df3eb7efd30495f98571fe to your computer and use it in GitHub Desktop.
Save mdcpepper/40113b6425df3eb7efd30495f98571fe to your computer and use it in GitHub Desktop.
{% extends "_layout" %}
{# Get all the categories to display #}
{% set allCategoriesById = entry.linkCategory.indexBy('id') %}
{# Get all the pages related to those categories #}
{% set allCategoryPages = craft.entries.section('pages').relatedTo(allCategoriesById).with(['pagesCategory']) %}
{# Group the pages by the categories ID's #}
{% set pagesByCategoryId = allCategoryPages|group('pagesCategory[0].id') %}
{% block content %}
<div class="row">
<div class="col W-12">
<p>{{ entry.title }}</p>
</div>
</div>
{% if pagesByCategoryId|length %}
<div class="row">
{# Loop over each of the categories we fetched at the beginning #}
{% for categoryId, category in allCategoriesById %}
{# Now loop over all the pages that were grouped by that category's id (if there are any) #}
{% if categoryId in pagesByCategoryId|keys %}
{# you can use {{ category.title }} etc in here too if you need to #}
{% for page in pagesByCategoryId[categoryId] %}
<div class="col">
<p><a href="{{ page.url }}">{{ page.title }}</a></p>
</div>
{% endfor %}
{% endif %}
{% endfor %}
</div>
{% endif %}
{% endblock %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment