Skip to content

Instantly share code, notes, and snippets.

@certainlyakey
Last active May 31, 2018 11:02
Show Gist options
  • Save certainlyakey/b8d2dbea7569f8248e7f to your computer and use it in GitHub Desktop.
Save certainlyakey/b8d2dbea7569f8248e7f to your computer and use it in GitHub Desktop.
Craft CMS - display categories related to an entry macro
{# usage: {{ siteMacros.related_categories( entry, true, 'rubrics', 'rubric-list', 'Рубрики:', '?news=true' ) }} #}
{# in main layout: {% import '_partials/macros' as siteMacros %} #}
{% macro related_categories(entryModel, isSection=false, catGroupName='categories', className='categories', headingText='', appendToURL='') %}
{% set categories = craft.categories.group(catGroupName).relatedTo(entryModel) %}
{% if categories | length %}
{% if isSection %}
<section class="{{ className }}">
<h1>{{headingText}}</h1>
{% endif %}
<ul{{ (isSection ? '' : ' class="' ~ className ~ '"')|raw }}>
{% for category in categories %}
<li><a href="{{ category.url }}{{appendToURL}}">{{ category.title }}</a></li>
{% endfor %}
</ul>
{% if isSection %}
</section>
{% endif %}
{% endif %}
{% endmacro %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment