使用Category標籤隱藏Jekyll部落格文章
阿新 • • 發佈:2019-01-24
通過簡單設定Jekyll的Category,實現隱藏部分部落格文章的功能,此方法同樣適用於Octopress。
首先開啟你的source/index.html
檔案:
---
layout: default
---
<div class="blog-index">
{% assign index = true %}{% for post in site.posts %} <!--遍歷所有post-->
{% assign content = post.content %}
<article>
{% include article.html %}
</article>
{% endfor %}
<div class="pagination">
{% if paginator.next_page %}
<a class="prev" href="{{paginator.next_page_path}}">← Older</a>
{% endif %}{% if paginator.previous_page %}
<a class="next" href="{{paginator.previous_page_path}} ">Newer →</a>
{% endif %}
</div>
</div>
<aside class="sidebar">
{% if site.blog_index_asides.size %}{% include_array blog_index_asides %}{% else %}{% include_array default_asides %}{% endif %}
</aside>
發現預設是通過{% for post in site.posts %}
來遍歷所有post的,將這個迴圈改為:
{% for post in site.categories.你希望顯示博文的category %}
這樣就只會預設在首頁顯示特定category的文章而隱藏其他文章了。
我的習慣時把想公開的文章的category設定成public,然後{% for post in site.categories.public %}
,就達到隱藏其他文章的目的了。