惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
Microsoft Azure Blog
Microsoft Azure Blog
L
LangChain Blog
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog
宝玉的分享
宝玉的分享
B
Blog RSS Feed
MongoDB | Blog
MongoDB | Blog
Jina AI
Jina AI
D
Docker
B
Blog
Engineering at Meta
Engineering at Meta
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
I
InfoQ
G
Google Developers Blog
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
大猫的无限游戏
大猫的无限游戏
阮一峰的网络日志
阮一峰的网络日志
U
Unit 42

Example blog

Ghost routing explained with routes.yaml examples — routes, collections & taxonomies Ghost redirects explained with practical redirects.yaml examples How to Serve JSON or XML Data in Ghost How to Change Your Default Post Template in Ghost Ghost Custom Theme Settings How to add Google Fonts to Ghost Themes How to Configure Ghost with Mailgun Ghost Theme Structure & Organization How to Make Custom Templates in Ghost Themes How to Install Ghost CMS via Ghost-CLI How to Add Syntax Highlighting to Ghost The best Ghost CMS hosting platforms Ghost 6.0 — Social Web and Native Ghost Analytics Ghost CMS Social web beta, email 2FA and more author social links How to keep custom design settings when updating your Ghost theme How to use the public preview card in Ghost How to add members-specific sections in Ghost How to customize the default content CTA in Ghost How to build a custom homepage in Ghost An overview of Ghost editor cards and and how they work Ghost CMS News: One-time payments & collecting sales tax How to customize the private site access template in Ghost How to add custom fonts in Ghost themes Ghost CMS News: Additional payment methods & internal linking How to use Code Injection in Ghost How to set meta data of your custom routes & collections in Ghost Ghost CMS News: TK reminders, ActivityPub and improvements How to format dates and use the date helper in Ghost Ghost CMS internationalization, custom fonts & spam protection Understanding and optimizing post and page settings in Ghost CMS
How to Create Related Posts Section in Ghost
Bright Themes · 2023-06-02 · via Example blog

A related posts, or a read next section is an essential component of any blog or website that aims to increase user engagement and provide a seamless browsing experience. By offering visitors relevant content based on their interests, you can extend their time on your site.

In this blog post, we will delve into the process of creating a related posts section in Ghost, but first let's discuss about the benefits.

Some of the key advantages include:

  1. Improved user experience: By suggesting relevant content, you enhance the browsing experience, making it easier for visitors to discover and access articles of interest.

  2. Increased engagement: Encouraging readers to explore more content can lead to longer session durations, decreased bounce rates, and a higher likelihood of conversion or interaction.

  3. Enhanced SEO: A well-implemented related posts section can contribute to improved search engine optimization by increasing the number of internal links and reducing the likelihood of orphaned pages.

  4. Cross-promotion: Utilize the related posts section to highlight older or lesser-known articles, giving them increased visibility and potentially driving traffic to older posts.

Now that we have an understanding of the benefits, let's delve into the technical part.

Identify where to implement the changes

To create a related posts section in Ghost, you'll need to make some modifications to your theme. Before making any changes, it's a good idea to create a backup of your theme to ensure you can revert to the original version if needed.

In general, in Ghost themes the post.hbs is responsible for rendering individual blog posts, locate the theme folder you are currently using and find the post.hbs file.

Within the file, you will see the opening and closing {{post}} tags, which gives us the context:

{{#post}}

  // your post layout here

{{/post}}

All the necessary changes for the related section can be done in this file.

Now that we have the place, let's get to the code part. Ghost provides a powerful Content API that allows you to fetch related posts based on various criteria, such as tags.

Ghost uses the Handlebars templating engine, so you can use its helpers and filters to filter and sort the posts.

You have two main options to get related posts:

  1. Based on the primary tag of your posts
  2. Based on all associated tags

In both cases we will be using the {{get}} helper.

If you are only interested in the primary tag (the first tag assigned to your posts), then you can use the following code inside the {{#post}} {{/post}} context:

{{#get
  'posts'
  include='tags,authors'
  limit='3'
  filter='id:-{{id}}+tag:[{{primary_tag.slug}}]'
}}

  <!-- Display related post details here -->

{{/get}}

As you can see in the code above, we are using some specific filters:

  • id:-{{id}} - to exclude the current post
  • tag:[{{primary_tag.slug}}] - to fetch posts that have the current posts' primary tag

It's also possible to get related posts for all tags that are associated with a post. For this add the following code after the closing {{/post}} tag:

{{#get
  'posts'
  include='tags,authors'
  limit='3'
  filter='id:-{{post.id}}+tags:[{{post.tags}}]'
}}

  <!-- Display related post details here -->

{{/get}}

As you can see, there are some minor differences compared to the precious code, this is because of the context:

  • id:-{{post.id}} - to exclude the current post
  • tags:[{{post.tags}}] - to fetch posts using all tags from the current post

Note

Use the limit property to change the maximum amount of posts that are fetched. Use the include property to add more context to the fetched posts.

Now that we have the data (posts) available, we can go on and display it. This can be done by adding HTML markup and CSS styling. In most themes there is a partial to render post cards in a specific way.

In our case, this is partials/post-card.hbs, assuming you have something similar, we can render the posts using this partial, by adding the following code inside the our previously defined {{get}}:

<section class="related-section">
  <h2 class="related-title">{{t "Read next"}}</h2>

  <div class="related-posts">

    {{#foreach related}}
      {{> post-card}}
    {{/foreach}}

  </div>
</section>

Within the {{#foreach posts}} block, you have each posts' details, which will be used to display various information about the post, for example, you can display the post title, excerpt, and a link to the post.

In our case what information is displayed is defined in the {{> post-card}} partial (partials/post-card.hbs).

To give you a complete example, for fetching 3 related posts based on the primary tag:

{{#get "posts" include="tags,authors" limit="3" filter="id:-{{id}}+tag:[{{primary_tag.slug}}]"}}

  <section class="related-section">
    <h2 class="related-title">{{t "Read next"}}</h2>

    <div class="related-posts">

      {{#foreach related}}
        {{> post-card}}
      {{/foreach}}

    </div>
  </section>

{{/get}}

In this guide we won't cover the styling, as that is specific to each theme, and in most cases the you can reuse class names to match the style of your current theme.

After following these steps, your Ghost CMS should display a related posts section on individual blog posts, showing posts that are related based on the criteria you defined.

Conclusions

By adding a related posts section to your Ghost blog, you can significantly enhance user engagement, improve the browsing experience, and drive more traffic to your content.

Follow the steps outlined in this guide to seamlessly integrate this feature into your Ghost theme and create a dynamic and engaging environment that keeps visitors exploring your content and coming back for more.