Skip to content

Instantly share code, notes, and snippets.

@umkasanki
Created December 6, 2018 08:55
Show Gist options
  • Save umkasanki/df48abd4f96f8be9fe1689bdd09c02a1 to your computer and use it in GitHub Desktop.
Save umkasanki/df48abd4f96f8be9fe1689bdd09c02a1 to your computer and use it in GitHub Desktop.
CraftCms. Get entries or products based on nested categories in url
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Category</title>
</head>
<body>
{% set catSegments = craft.request.segments|slice(1) %}
{% for item in catSegments %}
{% if craft.categories.slug(item).one() == null %}
{% exit 404 %}
{% endif %}
{% endfor %}
{% set relationParam = ['and'] %}
{% set relations = [] %}
{% set heading = '' %}
{% set categoriesArr = craft.categories()
.slug(catSegments)
.orderBy('groupId asc')
.all() %}
{% for category in categoriesArr %}
{% set relationParam = relationParam|merge([{ targetElement: category }]) %}
{% set heading = heading ~ (category.altTitle ?? category.title)|lower ~ ' ' %}
{% endfor %}
{% set productsQuery = craft.products({
type: 'footwear',
relatedTo: relationParam
}) %}
{% paginate productsQuery.limit(10) as pageInfo, pageEntries %}
{% if productsQuery is defined %}
{% for product in pageEntries %}
<li>
<a href="{{ product.url }}">{{ product.title }}</a></b>
</li>
{% else %}
<p>No results</p>
{% endfor %}
{% endif %}
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment