CATEGORIES

Outputting Category Fields in any Location

5min
this article shows you how you can access categories' fields on any liquid page if you're beginning to develop with categories, start here! 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 this time we'll look at the categories object this makes information about your site's categories available via liquid it can be accessed in any liquid file on siteglide we'll cover outputting the categories data object accessing a specific category via id after accessing the category, outputting its fields looping over all categories on the site outputting the categories data object the following liquid outputs a json object containing the details of all categories {{context exports categories data}} this object is a "key map" with every category stored in key value pairs where the key is the id of the category and the value is an object containing all other available fields accessing a specific category via id as the categories data object is a key map, you can access any specific category by accessing the object and then passing in the category's id in square brackets {{context exports categories data\['1234']}} accessing a category's fields once you've accessed the value of that category via it's id, you can access that category's fields using dot notation for example, here we'll access its name {{context exports categories data\['1234']}} name other available fields are id the id of the category name the name of the category parent the id of the parent category if there is one slug the end of the url for the category detail page which is unique to this item meta title the meta title of the category meta desc the meta description of the category og title the open graph title of the category og desc the open graph description of the category og type the open graph type of the category twitter type the twitter type of the category full slug the full (relative) url for the category detail page looping over all categories on the site if you wish to display all the categories on the site, you can loop over them all inside the for loop you can display any html or category fields you like {% for category in context exports categories data %} {{category\[0]}} {{category\[1]}} {{category\[1] name}} {% endfor %} if you want to skip any categories, you can use liquid if statements and the continue tag to do this {% for category in context exports categories data %} {% if category\[1] name == "name of category i want to skip" %} {% continue %} {% endif %} {{category\[1] name}} {% endfor %}