Realize that the prev and next pages only contain articles of the same category
2022-12-16 22:16 ≈ 653Words ≈ 4Minutes

Note

This article was automatically translated using Google

I upgraded hexo to 6.3.0 during the day, but it’s still boring at night, and the outside is still very dangerous, so I just put this function is realized, this problem has always existed, However, due to laziness, it dragged on for nearly a year.

Let me briefly introduce that my website has three navigations belonging to three categories, so when entering the article, the titles and links of the previous and next pages in the footer are actually in the order of date for all articles as a whole Arranged, so for example, if you click on the next page in the essay, you may click on the article in the experience sharing, which is a bit of a drama. Solve that problem tonight.

Since each person’s theme is different, the writing method of ejs corresponding to the article page may also be different, here is the original code to explain it (it’s ugly, definitely not the best practice, but it can be used, some masters see it and want to help I’ll optimize it and ask for a message [laughing])

I commented out the ejs syntax and html syntax, in order to better display the js code

This is written in the post.ejs file

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
var tmpNext = page.next; //Give the next page object of the current page to a variable
while(true){ //violent loop
if(tmpNext){ //When the next page is not empty
if(page.categories.data[0].name == tmpNext.categories.data[0].name){ //The first category of the current page is the same as the category of the next page
if(page.categories.data[0].name == 'novel'){ //The current page is a novel category
if(page.categories.data[1].name == tmpNext.categories.data[1].name){ //Add a second-level category judgment
// <a href="<%- url_for(tmpNext.path) %>" target="_self">
// <span><%= tmpNext. title %></span>
// <i class="iconfont icon-chevronright"></i>
// </a>
break;
}else{
tmpNext = tmpNext.next; //Find the next big complaint
}
}else{
// <a href="<%- url_for(tmpNext.path) %>" target="_self">
// <span><%= tmpNext. title %></span>
// <i class="iconfont icon-chevronright"></i>
// </a>
break;
}
}else{
tmpNext = tmpNext.next; //Find the next big complaint
}
}else{ //The next page is empty and exit the loop
break;
}
}

I found that it is not clear to use the source code, it is also because I wrote too violently, let me briefly explain:

  1. First judge whether there is a next page
  2. Then judge whether the classification of the current page and the next page are consistent, if not, find the next big complaint
  3. When the current page is consistent with the next page, then judge whether it is a novel category, if not, it will be it directly
  4. When the current page is a novel classification, then judge the second-level classification (my novels are stored in collections according to the second-level classification), if not, find the next big complaint
  5. The current page is a novel category, and it belongs to a collection, that’s it

This perfectly fulfills my needs:

  1. Essays and experience classification All articles are linked to the next page
  2. Classification of novels is linked to the next page by collection

I think there is a better way of writing, and other topics should have the same needs as mine, but since I like this topic, I am too lazy to study other topics. After all, the most important thing about a personal website is the content, so use it first if it can satisfy the function.