article.categories

Previous page  First page  Next page

article.categories is a loop used to generate the list of categories under which the article was posted. In the default article template, this list appears immediately after the body of the article.

 

The basic syntax of the article.categories loop is:

 

{{loop article.categories}}

(body of loop goes here)

{{/loop}}

 

The loop does one iteration for each category under which the article was posted. For example, if the currently viewed article was posted under categories Archery, Books and Gardening, the loop will go through Archery, then Books, then Gardening.

 

Within the article.categories loop, you can use these variables, whose contents change with each iteration:

 

label

The label of the category for the current iteration. The label of a category is the name that is displayed to the blog reader.

 

url

The URL of the page for the category for the current iteration.

 

 

 

Example 1

 

Here is an example article.categories loop:

 

<h3>This article was posted under the following categories:</h3>

<ul>

{{loop article.categories}}

       <li><a href="{{url}}">{{label}}</a></li>

{{/loop}}

</ul>

 

For an article posted under the categories Archery, Books and Gardening, the output of the above loop would look like this:

 

This article was posted under the following categories:

Archery
Books
Gardening

 

In the example above, each category is linked to the corresponding category page.

 

 

The breadcrumb Loop within article.categories

 

You can also use a loop within the article.categories loop: the breadcrumb loop. This loop lets you generate breadcrumbs -- a "trail" of categories leading back to the Main Page category -- for the current category.

 

Within the breadcrumb loop, you can use these variables, whose contents change with each iteration:

 

label

The label of the category for the current iteration. The label of a category is the name that is displayed to the blog reader.

 

url

The URL of the page for the category for the current iteration.

 

 

Example 2

 

The following example illustrates the use of the breadcrumb loop within the article.categories loop by expanding on the example shown earlier:

 

{{loop article.categories}}

<li>

<a href="{{url}}">{{label}}</a> (

{{loop breadcrumb}}

<a href="{{url}}">{{label}}</a>

{{unless __Last__}}

>>

{{/unless}}

{{/loop}}

)

</li>

{{/loop}}

 

Given that:

The category Archery is a subcategory of Sports, which is a subcategory of Hobbies, which is a subcategory of Main Page.
The category Books is a subcategory of Main Page.
The category Gardening is a subcaetgory of Hobbies, which is a subcategory of Main Page.

 

In such a case, the code shown above would generate the following output:

 

This article was posted under the following categories:

Archery ( Main Page >> Hobbies >> Sports >> Archery )
Books ( Main Page >> Books )
Gardening ( Main Page >> Hobbies >> Gardening )