Hexo-theme-next: 是否可以在主页只显示特定category下的文章

Created on 23 Apr 2016  ·  8Comments  ·  Source: iissnan/hexo-theme-next

你好,iissnan
谢谢你制作出这么好的主题。
我有一个想法就是是否可以在主页只显示特定category下的文章呢?这样就可以控制在home 首页下控制想要突出展示的文章。比如博客下混合了技术类和生活类文章,我可以选择只在首页展示技术类的,而其他类别的文章可以到category里面点击查看。这样就不用开两个博客再链接了。

谢谢 :)

http://forwardkth.github.io/

Most helpful comment

楼主给的是增加了一个控制“是否显示到首页”的变量
我这里给个通过tag来控制的方案,便于在tag标签页里统一展示
同样是在index.swig里修改

  <section id="posts" class="posts-expand">
    {% for post in page.posts %}
        {% for tag in post.tags %}
            {% if tag.name=='首页' %} 
                {{ post_template.render(post, true) }}
            {% endif %} 
        {% endfor %}
    {% endfor %}
  </section>

到文章正文里添加

tags:
- 首页

即可

All 8 comments

我也比较想首页可以定制显示

https://github.com/iissnan/hexo-theme-next/issues/474 说了个大概

应该在themes下的layout中的index.swig中添加

楼主给的是增加了一个控制“是否显示到首页”的变量
我这里给个通过tag来控制的方案,便于在tag标签页里统一展示
同样是在index.swig里修改

  <section id="posts" class="posts-expand">
    {% for post in page.posts %}
        {% for tag in post.tags %}
            {% if tag.name=='首页' %} 
                {{ post_template.render(post, true) }}
            {% endif %} 
        {% endfor %}
    {% endfor %}
  </section>

到文章正文里添加

tags:
- 首页

即可

@chenkaiwei 请问这个可以用分类(category)来控制吗? 谢谢!

Thanks for the solution provided by @forwardkth and @chenkaiwei !

But it's not perfect. The pagination still treat all articles as before, so if you have a sequence of hide articles you may got an empty page.

@forwardkth @chenkaiwei @fuyufjh 是的,如果直接这样改的话,隐藏的文章多了,首页会显示空白,因为分页计数并没有忽略掉这些隐藏的文章

@newdee @forwardkth @chenkaiwei I solved this issue by following workaround.

In <your_hexo_blog>/node_modules/hexo-generator-category/lib/generator.js

Add one line to filter out some articles

module.exports = function(locals) {
  var config = this.config;
  var posts = locals.posts.sort(config.index_generator.order_by);
  var paginationDir = config.pagination_dir || 'page';
  var path = config.index_generator.path || '';

  // Only show 'selected' some articles
  posts = posts.filter(post => post.tags.some(tag => tag.name.toLowerCase() === "selected"));

  return pagination(path, posts, {
    perPage: config.index_generator.per_page,
    layout: ['index', 'archive'],
    format: paginationDir + '/%d/',
    data: {
      __index: true
    }
  });
};

Then, add selected tag to your articles which you wish to show on homepage.

Be award that this change may not be uploaded to you git repo, if you manage your Hexo posts with GitHub or else. Not perfect again.😅

Wish this helpful to you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

keltoy picture keltoy  ·  3Comments

chy9002 picture chy9002  ·  3Comments

Nirvanada picture Nirvanada  ·  4Comments

ifyour picture ifyour  ·  4Comments

syscca picture syscca  ·  4Comments