certainlyakey
7/13/2015 - 11:03 AM

Craft CMS - display categories related to an entry macro

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 %}