A category loop lets you loop through children categories of a particular category or subcategory.
Example:
{% loop category(cat_mens) %} <li> <a href="{cat_products.url}">{cat_products.name}</a> </li> {% endloop cat_products %}
The above example will loop through all the subcategories based on hierarchy of cat_products. For example, lets say cat_mens had the children of T-Shirts and Pants. This will loop will return 2 return results of T-Shirts and Pants. However, lets say you T-Shirts had 3 subcategories of Long Sleeve, Short Sleeve and V-Neck. These will not be return because the category loop only loops 1 level down. If you want to loop through those, you will need to pass the slug for T-Shirts. By default, the ordering of the result set will be based on hierarchy of sibling records. For example, if T-Shirt is specified before Pants, T-Shirt will show up first.
If you do not want to specify the page, you can use the slug "
current". This will specify the current page as the slug.
Example:
{% loop category(current) %} <li> <a href="{cat_products.url}">{cat_products.name}</a> </li> {% endloop cat_products %}
If you want to change the order of the output to something else such as alphabetical, you can specify the loop params by specify 2 more parameters.
{% loop category(current,name,asc) %} <li> <a href="{cat_products.url}">{cat_products.name}</a> </li> {% endloop cat_products %}
The following parameters are available
Parameter | Required | Type | Description |
1 | Yes | Filter On | This can either be a category slug, subcategory slug or current |
2 | No |
Sort On
|
What the category loop sorts on. This can be hierarchy, name or id. By default, it is hierarchy. This is most common.
|
3 | No |
Order By
|
"asc" for ascending or "desc" for descending order. This parameter only works on id or name sort on parameter 2.
|
Below you will find an example on using a conditional inside the category loop to specify a current page being viewed.
{% loop category(cat_products) %} <li> <a href="{cat_products.url}" class="link {% if page_slug == 'page_examples_1' %} current {% endif %}">{cat_products.name}</a> </li> {% endloop cat_products %}
NOTE: current can only be used on a category or subcategory page.
Here are the following output tags available with this loop.
Output Tag | Description |
{name} | Category Name |
{url} | URL of the category or subcategory |
{page_name} | Name of Page |
{page_slug} | Slug of Page |
{page_description_7} | First 7 words of page content description |
{page_description_10} | First 10 words of page content description |
{page_description_15} | First 15 words of page content description |
{page_description_25} | First 25 words of page content description |
{page_description_50} | First 50 words of page content description |
{page_description_75} | First 75 words of page content description |
{page_description_100} | First 100 words of page content description |
{page_description_200} | First 200 words of page content description |
{page_description} | First 200 words of page content description |
{product_url} | Product URL |
{has_product_image} | Boolean (true/false) if product has an image or not |
{product_image_[size]} | This will call the default image for the product. See the section about image sizes to enter for the word [size] |
{product_image_[size]_url} | This will call the url for the default image for the product. See the section about image sizes to enter for the word [size] |
attribute slug | All page attribute slugs that have been flagged to show externally |
Loop Conditionals | Click here to learn more about loop conditionals. |
Please see the template loop for pages for all available variables for a category loop.
This loop does not have access to pagination. This function also has access to the variable in product loop “has_product_image" and the default product image for that category.
NOTE: Additionally, another option that is available if you would like to approach specific orders of products appearing on a category page, this could be accomplished using custom drop down attribute and then writing a custom loop to loop through the attribute values to control the order - otherwise the options are ASC or DSC orders based on the database IDs.