hexojs / hexo

A fast, simple & powerful blog framework, powered by Node.js.
https://hexo.io
MIT License
39.2k stars 4.82k forks source link

Previous/next post with category filter? #2270

Closed timpulver closed 1 year ago

timpulver commented 7 years ago

Hi,

I know I can display the previous and next post on a post page using something like this:

<% if (post.next){ %>
    <a href="<%- url_for(post.next.path) %>" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption"><%= __('older') %></strong>
      <div class="article-nav-title"><%= post.next.title %></div>
    </a>
  <% } %>

Is it possible to use a category filter with this? I have posts with category artist and of category project. If on a project site I would like a next / previous button to link to the next project only, not artist.

Thanks!

NoahDragon commented 7 years ago

I don't think Hexo gonna support it. The downside I could see is the number of static files will be overwhelming.

timpulver commented 7 years ago

The site I’m building does not have that many projects / artists, so I think this should be okay.

If this is not possible, grabbing all posts, and comparing the create date to find the last / next one will probably work I guess, but I hoped there was an easier way.

NoahDragon commented 7 years ago

I think this related to your another issue. https://github.com/hexojs/hexo/issues/2269 Could we just keep this one open and discuss the solution?

For now, I have to take a look the Hexo document to see if there is a way to suit your scenario. Or maybe it is a feature request.

timpulver commented 7 years ago

Feel free to close this one / the other one. Thanks for your help!

stevenjoezhang commented 4 years ago

The data type of post.prev is also post (it's like a linked list), so you can write like this

let target = post.next;
while (target.categories.data[0].name !== 'your_category' && target.next) {
  target = target.next;
}
stale[bot] commented 4 years ago

This issue has been automatically marked as stale because lack of recent activity. It will be closed if no further activity occurs. Thank you for your contributions.