Download

How to display posts connected with a specific tag name?

Sometimes you need to create sections on your website, which will be used to display posts from specific tags. With the new rendering engine, introduced in Publii v.0.18.2 it is finally possible.

All we need is an active rendering of the @website.contentStructure global variable (it can be disabled in your theme for faster website rendering).

To render posts connected with a selected tag we need to use the following code:

{{#each @website.contentStructure.tags}}
  {{#checkIf this.name '==' 'TAGNAME'}}
    {{#if this.postsNumber}}
    {{#each this.posts}}
      {{title}}
    {{/each}}
    {{else}}
      There are no posts connected with TAGNAME :(
    {{/if}}
  {{/checkIf}}
{{/each}}
  

If you want to limit the number of rendered posts, please remember that you can use the @index variable:

{{#each @website.contentStructure.tags}}
  {{#checkIf this.name '==' 'TAGNAME'}}
    {{#if this.postsNumber}}
    {{#each this.posts}}
      {{#checkIf @index '<' 5}}
        {{title}}
      {{/checkIf}}
    {{/each}}
    {{else}}
      There are no posts connected with TAGNAME :(
    {{/if}}
  {{/checkIf}}
{{/each}}
 

Above code will render up to five posts connected with a specified tag name.

What are you waiting for?

Start building your site today.

  1. 1 Download Publii
  2. 2 Write your content
  3. 3 Publish your site
Create website