Hexo: Getting a list of posts in a specific category

Created on 19 Jan 2018  ·  4Comments  ·  Source: hexojs/hexo

Environment Info

Node version(node -v): v9.2.0

Your site _config.yml (Optional): http://raybo.org/fid

For question

I'm trying to display a list of posts on a sidebar for a single category. I've tried to do this in multiple ways, but I don't see an option in the documentation. I can get to the category object, but it just looks weird and I can't get to the name.

http://www.raybo.org/fid/

image

Right now, it just displays the list of ALL the posts regardless of category using list_posts

  <%-
   list_posts({
    count:5,
    orderby: "date",
    class: "sidebar-module-list",
    order:-1,
  }) %>

I've tried using list categories inside list posts in the transform.

    transform: function(value) {
      console.log(list_categories());
      return '<i class="fas fa-briefcase"></i> ' + page.categories + '--' + value;
    }

But that just lists all of the categories on the site.

If I try to go through each category to isolate it's name

console.dir(category.data) returns something like this:

[ Document {
name: 'assignments',
_id: 'cjcgudl180004m51tcmq3utts',
slug: [Getter],
path: [Getter],
permalink: [Getter],
posts: [Getter],
length: [Getter] } ]

But then, if you try to access cat.name you get 'undefined'. I've tried using pagination, list_posts, and go through each individual post. I feel like there's an easy way of doing this I'm missing or that there is just no way of doing this, but then...how are the individual category pages. Hopefully you can help before I go insane.

stale

Most helpful comment

I found this solution on stack overflow. The function is mentioned on hexo warehouse.
I used the function on this page. It lists the posts of the "python" category and sorted posts by title. Code: https://github.com/huyingjie/hexo-theme-A-RSnippet/blob/master/layout/list-category-leetcode.ejs

All 4 comments

You can achieve by using findOne function.
For example, I want to list posts in Python category.

<% site.categories.findOne({name: "python"}).posts.sort('title', 1).each(function(post) {%>
              <%= post.title %> <br>
 <% })%>

I'm going to try that out, but I don't see that anywhere in the documentation. Do you know where it is? Thanks.

I found this solution on stack overflow. The function is mentioned on hexo warehouse.
I used the function on this page. It lists the posts of the "python" category and sorted posts by title. Code: https://github.com/huyingjie/hexo-theme-A-RSnippet/blob/master/layout/list-category-leetcode.ejs

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

leoli-dev picture leoli-dev  ·  3Comments

lushijie picture lushijie  ·  3Comments

hjmJhon picture hjmJhon  ·  3Comments

Helihua1992 picture Helihua1992  ·  3Comments

hgDendi picture hgDendi  ·  3Comments