TransWikia.com

Display entry category

Craft CMS Asked by Andy Kirkpatrick on April 3, 2021

I know this is a really suck egg question, but I finding the Craft docs far less straightforward than EE (I’m just switching over), and so I’m struggling with the easy task of displaying an entry’s category.

{% for entry in craft.entries.section('blog').limit(1) %}
   <h2>{{ entry.title }}</h2>
                              
   
   {% if entry.byLine %}
   <h3>{{ entry.byLine }}</h3>
   {% endif %}
                           
   <h3>Category: { ???? } </h3>
   
   {% endfor %} 

Can anyone help?

One Answer

To display a single category you'll need to do something like this:

{% set category = entry.categoryfieldhandle.one() %}
{{category.title}}

To display multiple categories:

{% set categories = entry.categoryfieldhandle.all() %}
{% for category in categories %}
{{category.title}}
{% endfor %}

Replace categoryfieldhandle with the handle you used for your field.

You can find more in depth and alternative ways in the docs:

https://craftcms.com/docs/3.x/relations.html

Since you're new to craft I recommend taking a look at the following course https://craftquest.io/courses/craft-cms-3-tutorials to help get you started.

Correct answer by Steven Vandemoortele on April 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP