
Hexo 分类中上一篇、下一篇文章
在“还有书籍”主题中,我把分类当作一本书,分类中的文章则形成一本书的内容。
这时,不能简单的调用全站的文章来当作上一篇、下一篇的内容。
所以结合 Hexo 基于分类输出文章 - 折影轻梦 (nexmoe.com) 我写了一个基于分类输出上一篇、下一篇文章。
直接贴代码了,看注释应该能看懂
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<nav class="post-nav">
<% site.categories.map(function(category){ %>
<% page.categories.map(function(page_category){ %>
<% if(page_category.name == category.name){ %> <!-- 先找到与当前文字相同的目录 -->
<% let i = 0;%>
<% category.posts.sort('-date').map(function(post){ %>
<% i++; %>
<% if(post.title == page.title){ %> <!-- 在找到当前文章所在的 index -->
<% let ix = 0;%>
<% category.posts.sort('-date').map(function(post){ %>
<% ix++; %>
<% if(ix == i + 1 && post.title){ %> <!-- 上一篇文章 -->
<div class="old">
<span>上一章</span>
<a href="<%- url_for(post.path) %>"> <%= post.title %></a>
</div>
<% } %>
<% if(ix == i - 1 && post.title){ %> <!-- 下一篇文章 -->
<div class="new">
<span>下一章</span>
<a href="<%- url_for(post.path) %>"> <%= post.title %></a>
</div>
<% } %>
<% }) %>
<% } %>
<% }) %>
<% } %>
<% })%>
<% }) %>
</nav>
我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=28l9ouqqg7vo4