GitHub Download

{{#getPostsByTags}} helper

{{getPostsByTags}} is a helper used to retrieve post data regardless of the current context and filter them to posts which uses a specific tag.

This helper since Publii 0.34 have a improved and more powerful syntax. Read more.

Syntax:

{{#getPostsByTags NUMBER_OF_POSTS TAG_ID "EXCLUDED_POST_ID_1,EXCLUDED_POST_ID_2"}}
...
{{/getPostsByTags}}

Example:

{{#getPostsByTags 5 1 "3,4"}}
<li>
    <h2>{{ title }}</h2>
    <div>{{{ excerpt }}}</div>
</li>
{{/getPostsByTags}}

Above example will show up to 5 posts, which uses tag with ID = 1, excluding posts with post ID equal to 3 or 4.

Posts are ordered by the same order as specified in the site settings.

You can also use tag slugs:

{{#getPostsByTags 5 "lorem,ipsum" ""}}
<li>
    <h2>{{ title }}</h2>
    <div>{{{ excerpt }}}</div>
</li>
{{/getPosts}}

In the above example, code will show up to 5 posts, which uses tags with slug equal to "lorem" or "ipsum". No posts will be excluded.

If the number of posts will be set to -1 then all posts with given tags will be returned.

IMPORTANT: It requires availability of the @website.contentStructure global variable

New syntax available since Publii 0.34

Since Publii 0.34 this helper supports new syntax with querystring instead of many arguments:

{{#getPostsByTags "count=5&allowed=hidden,featured&tag_as=id&tags=1,2,3&excluded=1,2&offset=10&orderby=modified_at&ordering=asc"}} 
<h2>{{ title }}</h2>
<div>{{{ excerpt }}}</div>
{{/getPostsByTags}}

Query string param supports the followin options:

  • count - how many posts should be included in the result
  • allowed - which post statuses should be included
  • tags - which tags should be used
  • excluded - which posts should be excluded
  • offset - how many posts to skip
  • orderby - order field
  • ordering - order direction
  • tag_as - specify if we select by tag id or slug

It is possible to combine querystring options with theme option values using the concatenate helper:

{{#getPostsByTags (concatenate "count=" @config.post.amountOfPosts)}} 
<h2>{{ title }}</h2>
<div>{{{ excerpt }}}</div>
{{/getPostsByTags}}

 

Subscribe

Get the latest Publii news, updates and more delivered directly to your email inbox

You can change your mind at any time by clicking the unsubscribe link in the footer of any email you receive from us, or by contacting us at contact@tidycustoms.net. By clicking below, you agree that we may process your information in accordance with our Privacy Policy.