GWcode Categories Examples

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

Example 9 - Showing child categories or parent categories

Here's the category group I'm going to use for these examples:

  • cat1
    • cat1_1
      • cat1_1_1
      • cat1_1_2
        • cat1_1_2_1
    • cat1_2
  • cat2
    • cat2_1

Child categories

Getting child categories for a category is easy. To get the child categories for cat1, you'll only have to provide the cat_id parameter with the category ID (13 in my case):

{exp:gwcode_categories cat_id="13"}
	{cat_name}
{/exp:gwcode_categories}

Or, you can use the cat_url_title parameter. Let's say that your URL looks like this:
http://domain.com/group/template/services
where "services" is the category (segment_3). Your code could then be:

{exp:gwcode_categories cat_url_title="{segment_3}"}
	{cat_name}
{/exp:gwcode_categories}

The results will be a list like this:

  • cat1
    • cat1_1
      • cat1_1_1
      • cat1_1_2
        • cat1_1_2_1
    • cat1_2

If you don't want to show the "cat1" category, you can add the incl_self parameter like so: incl_self="no". And, if you don't want to show the "cat1_2" category for example, you can use the excl_cat_id parameter to remove it. As always, you can use the depth, min_depth or max_depth parameters as well, to only show those categories with a certain depth.

If you're using the cat_url_title parameter and 2 or more category groups have a category with the same category url title, you may want to add in the group_id parameter to make sure the plugin isn't selecting the category from the wrong category group.

Parent categories

Getting a category's parent categories is just as easy. To get the parent categories for cat1_1_2_1, you use the cat_id or cat_url_title parameter in combination with the show_trail parameter like so (where 26 is my ID for cat1_1_2_1):

{exp:gwcode_categories cat_id="26" show_trail="yes"}
	{cat_name}
{/exp:gwcode_categories}


The results will look like this:

  • cat1
    • cat1_1
      • cat1_1_2
        • cat1_1_2_1


In most cases, you'll probably want to use this to create a breadcrumb trail from the root category to the category you provide. So, you'll want to use the style parameter to make the list linear and the backspace parameter to remove the last devider:

{exp:gwcode_categories cat_id="26" show_trail="yes" style="linear" backspace="7"}
	{cat_name} »
{/exp:gwcode_categories}

This will then be the output:

cat1 » cat1_1 » cat1_1_2 » cat1_1_2_1