An ExpressionEngine plugin making it easy to build URLs and breadcrumbs for categories on sites that have multiple levels of categories.
Unzip or clone the plugin directory down. From there installation is as per any other EE plugin. Copy the plugin dir (cat_trail) into system/expressionengine/third_party/.
Getting the URL for a category is very simple using Cat_trail. In the below example you can see I'm building the href value for a given category. You need to supply the cat_id parameter or the cat_url parameter.
- cat_id: The cat_id of category you want to build the link for.
- cat_url: The category_url_title of the category you want to build the link for.
<a href="/categories/{exp:cat_trail:get_cat_url cat_id='{cat_id}'}">{cat_name}</a>
Would result in:
<a href="/categories/category-one/category-two/my-category">My Category</a>
But note, if you're using another plugin like child_categories, ExpressionEngine parses nested plugins from inward outwards, so you need to specify the outer plugin to parse first (with parse="inward") so that the category ID is available and processed for the plugin to use.
<!-- Output a link to each child category -->
{exp:child_categories parent="{last_segment_category_id}" show_empty="yes" parse="inward"} <!-- See? -->
{child_category_start}
<a href="{exp:cat_trail:get_cat_url cat_id='{child_category_id}'}" class="browseAll">
{child_category_name}.
</a>
{child_category_end}
{/exp:child_categories}
Once you are a number of categories in, producing breadcrumbs can be a bit of a pain. Using the get_cat_structure tag pair, you can get the category parents for the supplied category and output nice easy breadcrumbs.
- cat_id: The cat_id of category you want to get the lineage for.
- cat_url: The category_url_title of the category you want to get the lineage for.
<!-- Output breadcrumbs for the current category -->
<ul class="breadCrumb">
{exp:cat_trail:get_cat_structure cat_id="{last_segment_category_id}" parse="inward"} <!-- Need this so it runs first -->
<!-- To build the proper full category URL we can call the get_cat_url method again -->
<li {if {last_segment_category_id} == {category_id}}class="current"{/if}>
<a href="/products/{exp:cat_trail:get_cat_url cat_id='{category_id}'}">{category_name}</a>
</li>
{/exp:cat_trail:get_cat_structure}
</ul>