8

If I were to have entries of types: Book, Film, Note, Post and Bookmark. How would I automatically create a list of links (or a filter) to each entry type?

1 Answer 1

7

First, get your section. The following code will return you a SectionModel:

{% set section = craft.sections.getSectionByHandle('mySectionHandle') %}

Next, get the Entry Types off it. This will return you an array of EntryTypeModel objects:

{% set entryTypes = section.getEntryTypes() %}

From there you can loop through them to create your nav:

{% for entryType in entryTypes %}
    <a href="{{ url('types/'~entryType.handle) }}">{{ entryType.name }}</a>
{% endfor %}

You’ll want to create a Route which accepts those dynamic URLs (e.g. types/(slug)), and point it to a template like “types/_type.html”.

From that _type template, you can fetch entries of the requested type like this:

{% set type = craft.request.getSegment(2) %}
{% set entries = craft.entries.type(type) %}
8
  • Ah brilliant, I knew it couldn't be hard. Thanks! Aug 6, 2014 at 15:00
  • Oh wait actually, doesn't that find entries of a type? Instead of the avaiable entry types themselves. Ie. I need a list to Books, Films, Notes etc Aug 6, 2014 at 15:01
  • I do apologise, completely missed your point - have updated with what I think you're after and am off hunting down some relevant docs if I can find them... Aug 6, 2014 at 15:15
  • 2
    @ShaneHudson Just updated the answer to also show how to create the nav, and make it work. Aug 6, 2014 at 15:26
  • 1
    @joshangell haha, I’ve got enough points :) Aug 6, 2014 at 15:48

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.