How to create a post list from specific tags
Sometimes you may need to display a list of posts from a specific tag or tags into an article. It's possible to do this via a shortcode: [posts tags="x,y,z" count="5"] . To implement this we have to run through a few coding steps, as follows:
We enable the createContentStructure option in the rendering section of the config. json file:
Now we have to create/add the following code to our helpers.js file:
If your existing helpers file includes other bits of code then you'll have to combine them as follows:
Now we have to add the helper to the places where the content with shortcode should appear, so, for example, go to the post.habs file and edit it as follows:
Change the {{{text}}}
to {{{parseShortcode text}}}
Tips: You can find in our themes another helper {{{lazyLoadForContentImages text}}}
for loading the post image with lazy load, so we have to connect both of the helpers together. Thankfully, Handlebars offers support for subexpressions, which allows you to invoke multiple helpers within a single mustache. It's very easy to do:
Finally open/create a post and add the following shortcode to it: [posts tags="x,y,z" count="5"] where x,y,z are the tag names separated by a comma and the 5 is the number of displayed tags.
An interesting thing to note here is that if the tags specified contain the same post, that post will only be displayed once on the list, rather than having any duplicates in the listing.