Egbert Lin's Blog

“Life is not a race, but a journey to be savoured each step of the way” by Brian Dyson

Hexo NexT v8.x.x - Add recent posts

How to add recent posts to Next theme(v8.x.x)

If you have managed your blog for a long time, maybe you want to reach high impression, you can append Recent Post to sidebar. Viewers can see it whatever the article they read.

Create Custom File:

In the /blog/source/ path, establish _data/sidebar.njk file under source/ directory, and add contents into sidebar.njk as below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!-- recent posts -->
{%- if theme.recent_posts %}
<div class="links-of-blogroll motion-element {{ "links-of-blogroll-" + theme.recent_posts_layout }}">
<div class="links-of-blogroll-title recent-posts-title">
<i class="fa fa-history {{ theme.recent_posts_icon | lower }}" aria-hidden="true"></i>
{{ theme.recent_posts_title }}
</div>
<ul class="links-of-blogroll-list recent-posts-list">
{%- set posts = site.posts.sort('-date').toArray() %}
{%- for post in posts.slice('0', '5') %}
<li class="my-links-of-blogroll-item">
<a href="{{ url_for(post.path) }}" title="{{ post.title }}" target="">
{{ post.title }}
</a>
</li>
{%- endfor %}
</ul>
</div>
{%- endif %}

Custom File Support [1]

In /blog/themes/next, open theme config file (_config.yml) and use custom_file_path key word to search the section. Then, uncomment the sidebar: source/_data_sidebar.njk as below:

Modify the theme config file (_config.yml)

Let add some contents to enable this function. So open _config.yml and add three line:

1
2
3
recent_posts: true
recent_posts_title: Recently
recent_posts_layout: block

Redeploy your blog

After deploying the blog, you will see recent posts successfully.

References:
[1] https://theme-next.js.org/docs/advanced-settings/custom-files.html?highlight=sideb