Nexmoe

Nexmoe

一个开发者。关于勇敢与热爱,互联网/创造/赛博朋克
twitter
github

Hexo previous and next articles in the category.

In the "Books" theme, I treat categories as a book, and the articles within the categories form the content of the book.

At this point, it is not possible to simply call all the articles on the website as the content of the previous and next articles.

Therefore, combining with Hexo Based on Category Output Articles - Shadow Dream (nexmoe.com), I wrote a code to output the previous and next articles based on categories.

Here is the code, you should be able to understand it from the comments:

<nav class="post-nav">
<% site.categories.map(function(category){  %>
    <% page.categories.map(function(page_category){ %>
        <% if(page_category.name == category.name){ %> <!-- First find the directory that matches the current text -->
            <% let i = 0;%>
            <% category.posts.sort('-date').map(function(post){ %>
                <% i++;  %> 
                <% if(post.title == page.title){ %> <!-- Then find the index of the current article -->
                    <% let ix = 0;%>
                    <% category.posts.sort('-date').map(function(post){ %>
                        <% ix++; %> 
                        
                        <% if(ix == i + 1 && post.title){ %>  <!-- Previous article --> 
                            <div class="old">
                                <span>Previous Chapter</span>
                                <a href="<%- url_for(post.path) %>"> <%= post.title %></a>
                            </div>
                        <% } %>
                        
                        <% if(ix == i - 1 && post.title){ %> <!-- Next article --> 
                            <div class="new">
                                <span>Next Chapter</span>
                                <a href="<%- url_for(post.path) %>"> <%= post.title %></a>
                            </div>
                        <% } %>
                        
                    <% }) %>
                <% } %>
            <% }) %>
        <% } %>
    <% })%>
<% }) %>
</nav>
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.