Web Hosting from Bluehost. Sign-up here

How to display tags in each blog post with Perch

Posted on by Clive Walker in Perch

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" hidden="true" />

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 attribute 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.

Leave a comment

A simple question that helps prevent automated form fillers.

© 2017 Clive Walker