Does your site run on Perch CMS? Hire me to help. Perch CMS Development

How to display tags in each blog post with Perch

Posted on by Clive Walker in Perch

I wrote this post a while back. The content can still be relevant but the information I've linked to may not be available.

I saw this post about showing post tags in the post.html template on the Perch forum recently. The post explains how to display tags for a blog post in the post's body rather than, for example, in a sidebar.

I've used this method on my Clive Walker site where I display tags at the top of each post (here for example). So, I thought it might be useful to summarise the code:

At the top of my post.php page (or in a Perch layout), I create a variable called tags_html containing my post's tags (using the perch_blog_post_tags function, described here).

<?php PerchSystem::set_var('tags_html', perch_blog_post_tags(perch_get('s'), [], true)); ?>

In my post.html template, I have this code which displays the tags:

<perch:blog id="tags_html" encode="false" type="hidden" />

It may not be immediately obvious but this uses a modified version of the Perch post_tag_link.html template to display the tags in an unordered list. The modifications are a class on the <ul> (so that I can use a tag icon background SVG image on the list items in my CSS) and a cleaner post tag URL. Relatively minor changes.

The hidden value means that this tags field is not shown on the post editing screen (there's already a tags field as part of the default Perch functionality).

That's it. I think it's useful to display tags with the post's content so I'm sure I'll be using this again.

Does your site run on Perch CMS? Hire me to help. Perch CMS Development

Comments are OFF for this post.

© 2024 Clive Walker