Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

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) %}
share|improve this answer
    
Ah brilliant, I knew it couldn't be hard. Thanks! –  Shane Hudson Aug 6 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 –  Shane Hudson Aug 6 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... –  Josh Angell Aug 6 at 15:15
2  
@ShaneHudson Just updated the answer to also show how to create the nav, and make it work. –  Brandon Kelly Aug 6 at 15:26
1  
@joshangell haha, I’ve got enough points :) –  Brandon Kelly Aug 6 at 15:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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