4

I'm wondering if it's possible to dynamically filter a section list by entry type based on a url segment?

I'm hoping to avoid assigning a category to every entry.

3

Yes, it is possible.

Pass the entry type handle to a segment and access that segment using the craft.request getSegment() method. Use this segment variable with the craft.entries .type() method to fetch only the entries of the given type. The example below assumes your entry type handle is in segment 2.

{% set entries = craft.entries.section('pages').type(craft.request.getSegment(2)) %}

{% for entry in entries %}
    <li>{{ entry.title }}</li>
{% endfor %}
  • Would I need to set up a route in order to tell craft to use the template from the first segment? When I add another segment to the url I get a 404 error. – Jesse Bennett-Chamberlain Jun 24 '14 at 18:40
  • Great point. If the URL that loads the template is tied to a Single or Section entry, yes, you should define a route. – Alex Kendrick Jun 24 '14 at 19:41

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.