GWcode Categories Examples

Here are some examples to demonstrate what you can do with GWcode Categories:

Example 3 - Showing categories of any (fixed, minimum or maximum) depth

Selecting categories by depth has a huge advantage over selecting categories by ID as you would have to do with the standard EE tags, because it allows you to easily add or remove categories in the control panel whithout ever having to update your templates with the new category ID's to reflect the changes!

In this example, we are going to get the categories with a fixed depth of 1 or 2 and categories with a minimum depth of 4. They are highlighted in blue:

  • cat1 (depth: 1)
    • cat1_1 (depth: 2)
      • cat1_1_1 (depth: 3)
      • cat1_1_2 (depth: 3)
        • cat1_1_2_1 (depth: 4)
          • cat1_1_2_1_1 (depth: 5)
    • cat1_2 (depth: 2)
  • cat2 (depth: 1)
    • cat2_1 (depth: 2)

The code for a nested list of categories:

{exp:gwcode_categories channel="example" depth="1|2" min_depth="4"}
	{cat_name}
{/exp:gwcode_categories}

Or, for a comma seperated (linear) list:

{exp:gwcode_categories channel="example" depth="1|2" min_depth="4" style="linear" backspace="1"}
	{cat_name},
{/exp:gwcode_categories}

Alternatively, this would work as well (using conditionals instead of the backspace parameter):

{exp:gwcode_categories channel="example" depth="1|2" min_depth="4" style="linear"}
	{if cat_count == results_total}
		{cat_name}
	{if:else}
		{cat_name},
	{/if}
{/exp:gwcode_categories}

As always, you could also use the entry_id or group_id parameter instead of the channel parameter to show categories for an entry or category group(s) respectively.