New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to order posts by category? #3278

Open
esappear opened this Issue Oct 7, 2018 · 3 comments

Comments

2 participants
@esappear

esappear commented Oct 7, 2018

I've tried two ways.

  • sort by key
<% page.posts.sort('categories.data[0].name').each(function(post){ %>
    
 <% }) %>
  • sort by function
<% page.posts.sort(function(a, b) {
    return a.categories.data[0].name -  b.categories.data[0].name;
  }).each(function(post){ %>
 <% }) %>

However, it seems that Hexo Query Data's sort method doesn't support a callback as a parameter.

@YoshinoriN YoshinoriN added the question label Oct 7, 2018

@YoshinoriN

This comment has been minimized.

Show comment
Hide comment
@YoshinoriN

YoshinoriN Oct 7, 2018

Member

@esappear
How about this ?

<% site.categories.sort('name', 'asc').each(function(category) { %>
    <% category.posts.sort('date', 'desc').forEach(function(post) { %>
        <%- post.title %>
    <% }); %>
<% }); %>

But, practically algorithm will be more complex, If post has child category.
I think following source code will helpful.

https://github.com/LouisBarranqueiro/hexo-theme-tranquilpeak/blob/master/layout/all-categories.ejs

Member

YoshinoriN commented Oct 7, 2018

@esappear
How about this ?

<% site.categories.sort('name', 'asc').each(function(category) { %>
    <% category.posts.sort('date', 'desc').forEach(function(post) { %>
        <%- post.title %>
    <% }); %>
<% }); %>

But, practically algorithm will be more complex, If post has child category.
I think following source code will helpful.

https://github.com/LouisBarranqueiro/hexo-theme-tranquilpeak/blob/master/layout/all-categories.ejs

@esappear

This comment has been minimized.

Show comment
Hide comment
@esappear

esappear Oct 8, 2018

@YoshinoriN
Actually, I want all my posts to be ordered by category and displayed by pagination, such as 10 posts per page. And all the posts in one page should be divided into categories.

# Page 1 (10 posts per page)
Category 1:
    - Post 1
    - Post 2
    - Post 3

Category 2:
    - Post 4
    - Post 5
    - Post 6
    ...

I'd like to implement it in generator by passing the category-ordered posts to pagination.

pagination('categories', locals.posts.sort(config.category_generator.order_by || '-date'), {
    perPage: 10,
    layout: ['category', 'archive', 'index'],
    format: 'page/%d/',
    data: {
      category: null,
    }
  })

But I can't make locals.posts ordered by category.
Now my implementation is to use site.posts.data and lodash's orderBy/groupBy method in ejs template files. However, I don't think it is elegance enough. If I could pass the category-ordered posts to pagination and read the page.posts in template files, it will be better.

esappear commented Oct 8, 2018

@YoshinoriN
Actually, I want all my posts to be ordered by category and displayed by pagination, such as 10 posts per page. And all the posts in one page should be divided into categories.

# Page 1 (10 posts per page)
Category 1:
    - Post 1
    - Post 2
    - Post 3

Category 2:
    - Post 4
    - Post 5
    - Post 6
    ...

I'd like to implement it in generator by passing the category-ordered posts to pagination.

pagination('categories', locals.posts.sort(config.category_generator.order_by || '-date'), {
    perPage: 10,
    layout: ['category', 'archive', 'index'],
    format: 'page/%d/',
    data: {
      category: null,
    }
  })

But I can't make locals.posts ordered by category.
Now my implementation is to use site.posts.data and lodash's orderBy/groupBy method in ejs template files. However, I don't think it is elegance enough. If I could pass the category-ordered posts to pagination and read the page.posts in template files, it will be better.

@YoshinoriN

This comment has been minimized.

Show comment
Hide comment
@YoshinoriN

YoshinoriN Oct 10, 2018

Member

Hmm, I have no idea. We want a who more familiar about pagination.

Member

YoshinoriN commented Oct 10, 2018

Hmm, I have no idea. We want a who more familiar about pagination.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment