1. 程式人生 > >【Hexo】 設定首頁隱藏指定文章

【Hexo】 設定首頁隱藏指定文章

有時候我們可能只想在首頁顯示關於程式設計之類的內容,而個人日記之類的文章放在其他分類之下而不在首頁顯示。可以從、分類、標籤、歸檔中檢視文章。
原文地址:

自定義front-matter的引數

例如,自定義新增一個notshow引數,值為true,用來提供判斷

---
title: 《好好學習》—黃金思維圈
date: 2018-06-12 11:45:43
tags:
- read
categories:
- read
notshow: true
---

front-matter就是每次hexo new “post_name”建立的文章裡面的開頭。
建立的文章存放在hexo根目錄下的:source/_posts

修改主題的index.swig

主題可能各不一樣,但原理都是一樣的,我拿我使用的next主題來示範。
路徑:Hexo\themes\next\layout\index.swig

{% extends '_layout.swig' %}{% import '_macro/post.swig' as post_template %}{% import '_macro/sidebar.swig' as sidebar_template %}{% block title %}{{ config.title }}{% if theme.index_with_subtitle and config.subtitle %}
- {{config.subtitle }}{% endif %}{% endblock %}{% block page_class %}{% if is_home() %}page-home{% endif -%}{% endblock %}{% block content %} <section id="posts" class="posts-expand"> {% for post in page.posts %}{{ post_template.render(post, true) }}{% endfor %} </section> {% include
'_partials/pagination.swig' %}
{% endblock %}{% block sidebar %}{{ sidebar_template.render(false) }}{% endblock %}

修改這裡:

{% block content %}
  <section id="posts" class="posts-expand">
    {% for post in page.posts %}{{ post_template.render(post, true) }}{% endfor %}
  </section>

  {% include '_partials/pagination.swig' %}{% endblock %

改成:

{% block content %}
  <section id="posts" class="posts-expand">
    {% for post in page.posts %}{% if post.notshow != true %}{{ post_template.render(post, true) }}{% endif %}{% endfor %}
  </section>

  {% include '_partials/pagination.swig' %}{% endblock %}

在for迴圈迭代文章中判斷文章中的屬性notshow,如果不為true就打印出文章。所以在需要隱藏的文章front-matter中新增notshow:true就可以了。

新增自定義選單

比如我想在選單欄新增一個“閱讀”選項,但又不想新建自己一個頁面,於是可以直接使用分類的頁面。

建立新文章的時候直接指定categories: read配置

---
title: 《好好學習》—黃金思維圈
date: 2018-06-12 11:45:43
tags:
- read
categories:
- read
notshow: true
---

在git中使用hexo g命令,hexo會在根目錄/public/categrises下自動生成分類中的閱讀資料夾
然後,
配置主題配置檔案themes/_config.yml中新增以下程式碼(#號後為註釋內容)

menu:
  home: / || home
  about: /about/ || user
  tags: /tags/ || tags
  categories: /categories/ || th
  read: /categories/read  #指定分類中閱讀的路徑

想把read顯示為中文,可以去
next\languages\zh-Hans.yml
下修改對應的單詞