5

Each of my entry types have their own template.

The url is something like:

/foo/{entryTypeHandle}

And the route is:

'foo/(?P<routesEntryTypeHandle>[-\w]+)' => 'bar.html'

In the template I can get the section the entry type is in:

{% set section = craft.sections.getSectionById(5) %}

And I can get ALL the entry types:

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

And I can get all the entries of that entry type:

{% set entries = craft.entries.section(section).type(routesEntryTypeHandle) %}

But I can't seem to get the entry type's name.

In the Sections Service there's a method called getEntryTypesByHandle but that's not available in the model.

I could get the entry type name from the entries, but that relies on there actually being results, which isn't assured.

{{ entries.first.type.name }}

I must be missing an easier way. Anyone?

10

A simpler way to do this might be:

{{ entry.type.name }}
  • this is the correct answer. – ricricucit Feb 22 '18 at 18:38
8

Classic find the answer straight after posting.

The getEntryTypes method of the sections model allows you to choose how the array is indexed. This means you can do this:

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

Which means our entry type model can be accessed using:

{% set entryType = entryTypes[routesEntryTypeHandle] %}

And the name:

{{ entryType.name }}
  • Didn't work for me on Craft 3. – mateostabio Feb 12 '19 at 20:57

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.