Related Content Via Taxonomy Overlap in Statamic

In my todo list of things to work on with my new Statamic-based blog was the ability to show related blog posts based on tags. In ExpressionEngine, a tag feature requires a 3rd party plugin from the start. In Statamic, flexible taxonomies come out of the box.

I looked through the documentation for instructions on how to show related entries based on taxonomy overlap but didn’t find what I thought I was looking for. A quick Twitter DM later to Jack McDade gave my my answer, use the “conditions” parameter on the entries:listing tag. It turns out this very feature is in the “london-wild” theme that ships with Statamic.

Here’s how I implemented the “Related Posts” feature on my blog:

Line one is the opening entries:listing tag. At first I was cautious about putting it within my template that’s already displaying entry content but that’s just because I’m used to ExpressionEngine. In Statamic, there is no need for an entries:listing tag on detail pages. That happens automatically. Because of that, there’s no problem with tags clashing.

The conditions parameter is where the magic happens. We set up one condition for tags and then feed it a list of all tags assigned to the current page’s entry content. The single curly braces allow the variable to be both set by the page but then accessed by other tags on the same page. Another condition prevents the current page from being displayed by excluding it by page_url.

On lines 5 and 10 you’ll see a common ExpressionEngine-based technique for rendering opening and closing tags only at the proper position in a loop of results. If you’re used to #eecms, you’ll feel right at home.

That’s pretty much it! Tag-overlap based related entries listing out-of-the box in Statamic.


blog comments powered by Disqus