Hello everyone,
This is the fourth issue of Ghost Tips & Tricks. I will be happy to share with you some practical Ghost tips that I find useful. Brought to you by Aspire Themes .
In this issue, I will share with you how to add a custom tags page to show a list of all your tags. I will use the default Casper theme as an example.
1. Create the Tags Page Theme File
In your theme directory, create a new file and name it custom-tags.hbs
and then copy the following code inside it.
{{!< default}}
<header class="site-header">
<div class="outer site-nav-main">
<div class="inner">
{{> "site-nav"}}
</div>
</div>
</header>
{{#post}}
<main id="site-main" class="site-main outer" role="main">
<div class="inner">
<header class="post-full-header">
<h1 class="post-full-title">{{title}}</h1>
</header>
{{#get 'tags' limit='all' include='count.posts' order='count.posts desc'}}
{{#foreach tags}}
<a href='{{ url }}'>
<h2>{{ name }} <small>({{ count.posts }})</small></h2>
</a>
{{/foreach}}
{{/get}}
</div>
</main>
{{/post}}
In this code, the get helper used to get the blog tags. The include
attribute used to include the posts count. The order
attribute used to order the tags based on the number of posts attached to them.
Next, the foreach helper used to loop through each tag and render the list with the tag URL, title, and the posts count.
At this stage, you may need to do a server restart. This will help the Template
dropdown to show up in the next step.
2. Create the Tags Static Page
- Create a new page and call it Tags for example.
- From
Page settings
, select theTags
template from theTemplate
dropdown. - Publish the page.
This is an initial example of what you can do to the tags page. You can take the code further and show the tag image with a custom design.
As an example, you can create something like what I have done with the Krabi and Penang themes.
That’s it for today and I hope you find this useful…
Checkout previous parts of the Ghost Tips & Tricks series:
- Ghost Tips & Tricks #1 / Show Publish Date in the day time with AM and PM format.
- Ghost Tips & Tricks #2 / Ghost Admin Right to Left (RTL) Support.
- Ghost Tips & Tricks #3 / Deploy Your Ghost Theme Using Github Actions.
Also, I started a Ghost Websites Inspiration series share inspiring websites created with Ghost. I hope you can find it inspiring and useful too.
Stay safe!
Ahmad