Jekyll x Liquid 控制文章列表只显示特定类别的Post
2016-12-19
前段时间画了一些漫画,考虑把漫画相关的 Post 放到另一个页面。
这个实现还是挺简单的,直接循环 Post 里面特定分类下面的文章就是
{* for post in site.categories.Comic *}
<li class="ant-timeline-item">
<a href="{{ post.url}}">{{ post.title }}</a>
</li>
{* endfor *}
但是如果我想要在所有文章里面不显示特定 Tag 的 Post 该如何实现呢?
这个也很简单, 只需要立一个 Flag循环判断是否含有这个 Tag 就行 o(
 ̄▽ ̄
)o
{* for post in site.posts *}
{* assign flag = 0 *}
{* for tag in post.tags *}
{* if tag == TagName *}
{* assign flag = 1 *}
{* endif *}
{* endfor *}
{* if flag == 1 *}
{* continue *}
{* endif *}
<li><a href="">xxxxxx</a></li>
{* endfor *}
要注意的地方: TagName 不需要加单引号或者双引号
前段时间写了个给特定 post 加密的功能
后来发现 post 的 header 部分可以有很多用途, 就比如上文提到的过滤特定类别的 post
我们可以在 header 里面这么写
layout: post
title: 《10101》EP0:我太受欢迎了该怎么办
category : Comic
tags : [Comic, 10101]
hideinpostslist: true
设置一个控制是否显示的参数为true
然后在 post list 页面循环判断
{* for post in site.posts *}
{* if post.hideinpostslist == true *}
{* continue *}
{* endif *}
{* endfor *}
比上面那个方法简单多了, 不过每个 post 需要额外设置这个参数, 如果 post 数量多的话还是参考上面的方法
关于本文
文章标题 | Jekyll x Liquid 控制文章列表只显示特定类别的Post |
发布日期 | 2016-12-19 |
文章分类 | Tech |
相关标签 | #Jekyll #Liquid |