CATEGORIES

Outputting Categories on WebApp / Module / eCommerce Layouts

9min
this article shows how you can use the category array field to access the ids of the categories assigned to a webapp or module item introduction in this set of articles, we'll show you the liquid syntax needed to get the most out of categories on the front end the following features are available in webapp and module layouts only, as they require access to a particular item's category array field in module layouts, they'll only be available in the item liquid file using the category array field to access the ids of categories that have been assigned to the current item displaying all categories that belong to the current item displaying the first, last or nth category which belongs to the current item check if an item has an nth category assigned to it accessing the category array field each webapp or module item has a category array field stored inside the this object for webapps, you can access this in the layout file for modules, you can access this in the item liquid file in the layout folder {{this category array}} this will output the ids of categories which are assigned to the current item in an array e g if the item is assigned to categories with id 1 and 2 it will look like this \[ "1", "2" ] displaying all categories that belong to the current item to loop over all these category ids you can use a liquid for loop liquid {% for category in this category array %} {{category}} {% endfor %} once you have the category id, you can use the categories data object to access any other category fields, e g the name liquid {% for category in this category array %} {{context exports categories data\[category]}} {{context exports categories data\[category] name}} {% endfor %} displaying the first, last or nth category which belongs to the current item instead of using a loop, you can access a specific index of the category array field and use it to fetch a particular category id first in category array you can use the following to only output the name of the first category assigned to the item {{context exports categories data\[this category array first] name}} last in category array you can use the following to only output the name of the last category assigned to the item {{context exports categories data\[this category array last] name}} nth in category array you can use the following to only output the name of a specifc category assigned to the item, by its zeroed index, where 0 is the first and 1 is the second liquid {% assign categoryid == this category array\[1] %} {{context exports categories data\[categoryid] name}} check if an item has an nth category assigned to it if you want to make sure the category item in the array you are calling exists first, then you should do the following first category in the array liquid {% if this category array\[0] != blank %} {{context exports categories data\[this category array\[0]] name}} {% endif %} second category in the array (and so on) liquid {% if this category array\[1] != blank %} {{context exports categories data\[this category array\[1]] name}} {% endif %}