Show related posts in Hexo
It is always nice to point the readers of your blog’s articles to related posts, they might be interested in. They stay a little longer to understand what you have to offer and increases the likelihood that they become loyal readers, followers or subscribers. Related posts has become a standard on delivering news and posts.
In the default Hexo theme Landscape, on which this blog is based, there is no such function built in, but as the Hexo community is very busy, there are some plugins you can use.
Plugin: hexo-list-related-posts
This plugin, available at GitHub is pretty lean and generates a list of links to related posts based on tags. It just counts how often a tag is occuring and shows a list of related posts either by count descending or randomly.
Advantage:
- Easy and fast
Disadvantage:
- Necessity of a sophisticated tag system
- Technical approach
Plugin: hexo-related-posts
Sergey Zwezdin made much more effort in his solution. The plugins depends on statistic methodologies like Stemming and TF/IDF, provided by the Node library Natural. It has plenty setting options like weighting and reserved words in order to optimize results.
Advantages:
- Much better results
Disadvantages:
- Huge installation, because of many dependent Node modules
- Necessity of maintaining reserved words
- Technical approach
Manually Curated
One point, that no technical solution can achieve is: you can guide the reader through your blog, by pointing out posts, which doesn’t really belong to the topic, but tries to give him a wider perspective on your thoughts or work. This is only possible, if you link the related posts manually. Here is a way to implement the requirements…
The right place to store related posts is in the Frontmatter of your article. Create a list below the keyword related
and take the slug (name of the post file) of the posts you want to show below the article as entries:
1 | title: My New fancy Post |
In your article.ejs
add a new partial called related to the place where it should be shown under the content of the actual article:
1 | <article |
In the folder themes/landscape/layout/_partial/post
, where all partials are stored which belongs to posts, create the new partial file:
1 | <% if (post.related && post.related.length){ %> |
(Remove the comments, because they doesn’t belong to EJS)
In this partial we loop through the Frontmatter list of related posts, determine the post by the given slug and render an archive panel for each post.
The list site.posts
should always contain a slug just once, therefore getting an array of posts and looping is just a precuation.
What you are getting you can see below…
You can interact with this article (applause, criticism, whatever) by mention it in one of your posts, which will be shown here as a Webmention ... or you leave a good old comment with your GitHub account.
In case your blog software can't send Webmentions, you can use this form:
Webmentions
No Webmentions yet...
Comments