1

This may be a simple question but I am not sure how to approach it - again trying to work out best practice for these things!

I have a "single" at /our-work, which among other things displays a projects listing - basically a list of links to the full entry for that project.

I now wish to filter that listing by the category they are in. I also wish to update the URL to reflect that filter e.g. /our-work/branding.

I have got the below code - listing out all the projects with their links, and also outputting the category links for the filter. When the links are clicked I currently I get an error - unsurprising considering the page doesn't exist.

How can I best achieve both the filtering and the required url change? Any help or pointers gratefully received by a craft beginner!

{% extends "_layouts/main" %}

{% block main %}

{% set categories = craft.categories.group('projectType').level(1).all() %}

{% for category in categories %}
  <a href="/our-work/{{ category.slug }}">{{ category.title }}</a>
{% endfor %}

{% for project in craft.entries.section('projects').all() %}
  <a href="{{ project.url }}">
    {{ project.title }}
  </a>
{% endfor %}

{% endblock %}
1

When setting up the category, you should be able to specify the URI Format which in your case would be "our-work/{slug}" then setting the template field to point where ever your category template is (You could point this at the same template as the overview and do a some checks but if you're beginner you'd probably better off having them in separate files).

Once you're loading in the new template, you need to get the projects that are related to the category by doing:

{% set projects = craft.entries.section('projects').relatedTo(category).all() %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.