Tags in Post Listing
Tags is a common way to help your blog readers find similar posts. You can display a post’s tags on their own with perch_blog_post_tags()
. But at times we want to include each post’s tags in our post listing. We can acheive this with perch_blog_custom()
.
Here’s a typical blog post listing displaying posts in descending order using the default post_in_list.html
template (minus the paging options):
perch_blog_custom([
'template' => 'post_in_list.html',
'sort' => 'postDateTime',
'sort-order' => 'DESC',
]);
In order to include tags in the post_in_list.html
template, we add the each
callback option to our perch_blog_custom()
options and use perch_blog_post_tags()
to get the tags for each post. In this example we are using the default post_tag_link.html
template for our tag links.
perch_blog_custom([
'template' => 'post_in_list.html',
'sort' => 'postDateTime',
'sort-order' => 'DESC',
'each' => function($item) {
$item['tags'] = perch_blog_post_tags($item['postSlug'], 'post_tag_link.html', true);
return $item;
},
]);
Now that we have the tags for each post, we need to add one perch:blog
tag to the post_in_list.html
template to display them. The id
of this tag should be the same as the key of $item['tags']
.
<perch:blog id="tags" encode="false" />
The post tag links directs to archive.php?tag=tagSlug
by default, so make sure to set up an archive page for your blog.