分頁

對於許多網站(尤其是部落格),將文章的主要清單分成較小的清單並在多個頁面上顯示是很常見的。Jekyll 提供分頁外掛程式,因此您可以自動產生分頁清單所需的適當檔案和資料夾。

對於 Jekyll 3 或更高版本,在您的 Gemfile 和 _config.yml 中的 plugins 下包含 jekyll-paginate 外掛程式。對於 Jekyll 2,這是標準的。

分頁僅在 HTML 檔案中運作

分頁無法在 Jekyll 網站的 Markdown 檔案中運作。分頁在從 HTML 檔案(稱為 index.html)中呼叫時運作,該檔案可以選擇存在於子目錄中並透過 paginate_path 設定值在子目錄中產生分頁。

啟用分頁

若要為部落格上的文章啟用分頁,請在 _config.yml 檔案中新增一行,指定每頁要顯示多少個項目

paginate: 5

這個數字應該是您希望在產生網站中每頁顯示的文章最大數量。

您也可以指定分頁的目的地

paginate_path: "/blog/page:num/"

這會讀取 blog/index.html,將每個分頁傳送至 Liquid 中的 paginator,並將輸出寫入 blog/page:num/,其中 :num 是分頁頁碼,從 2 開始。
如果一個網站有 12 篇文章,並指定 paginate: 5,Jekyll 會在目標目錄中寫入 blog/index.html,其中包含前 5 篇文章,blog/page2/index.html,其中包含後 5 篇文章,以及 blog/page3/index.html,其中包含最後 2 篇文章。

不要設定永久連結

在部落格頁面的前置資料中設定永久連結會導致分頁中斷。只要省略永久連結即可。

類別、標籤和彙整的分頁

較新的 jekyll-paginate-v2 外掛程式支援更多功能。請參閱儲存庫中的 分頁範例此外掛程式不受 GitHub Pages 支援

可用的 Liquid 屬性

分頁外掛程式公開 paginator liquid 物件,其中包含下列屬性

變數 說明

paginator.page

目前頁數

paginator.per_page

每頁文章數

paginator.posts

目前頁面可用的文章

paginator.total_posts

文章總數

paginator.total_pages

頁數總數

paginator.previous_page

上一頁的頁數,如果沒有上一頁則為 nil

paginator.previous_page_path

上一頁的路徑,如果沒有上一頁則為 nil

paginator.next_page

下一頁的頁數,如果沒有下一頁則為 nil

paginator.next_page_path

下一頁的路徑,如果沒有下一頁則為 nil

分頁不支援標籤或分類

分頁會瀏覽 posts 變數中的每篇文章,除非文章的前置設定中有 hidden: true。目前不允許對由共同標籤或分類連結的文章群組進行分頁。它無法包含任何文件集合,因為它僅限於文章。

呈現分頁的文章

接下來你需要做的是使用現在可用的 paginator 變數,以清單方式實際顯示你的文章。你可能會想要在網站的主要頁面之一中執行此操作。以下是一個在 HTML 檔案中呈現分頁文章的簡單範例

---
layout: default
title: My Blog
---

<!-- This loops through the paginated posts -->
{% for post in paginator.posts %}
  <h1><a href="{{ post.url }}">{{ post.title }}</a></h1>
  <p class="author">
    <span class="date">{{ post.date }}</span>
  </p>
  <div class="content">
    {{ post.content }}
  </div>
{% endfor %}

<!-- Pagination links -->
<div class="pagination">
  {% if paginator.previous_page %}
    <a href="{{ paginator.previous_page_path }}" class="previous">
      Previous
    </a>
  {% else %}
    <span class="previous">Previous</span>
  {% endif %}
  <span class="page_number ">
    Page: {{ paginator.page }} of {{ paginator.total_pages }}
  </span>
  {% if paginator.next_page %}
    <a href="{{ paginator.next_page_path }}" class="next">Next</a>
  {% else %}
    <span class="next ">Next</span>
  {% endif %}
</div>
小心第一頁的特殊情況

Jekyll 沒有產生「page1」資料夾,因此當產生 /page1 連結時,以上的程式碼將無法運作。如果你遇到這個問題,請參閱以下內容以了解如何處理。

以下 HTML 片段應該可以處理第一頁,並呈現每一頁的清單,其中包含連結至所有頁面(目前頁面除外)。

{% if paginator.total_pages > 1 %}
<div class="pagination">
  {% if paginator.previous_page %}
    <a href="{{ paginator.previous_page_path | relative_url }}">&laquo; Prev</a>
  {% else %}
    <span>&laquo; Prev</span>
  {% endif %}

  {% for page in (1..paginator.total_pages) %}
    {% if page == paginator.page %}
      <em>{{ page }}</em>
    {% elsif page == 1 %}
      <a href="{{ '/' | relative_url }}">{{ page }}</a>
    {% else %}
      <a href="{{ site.paginate_path | relative_url | replace: ':num', page }}">{{ page }}</a>
    {% endif %}
  {% endfor %}

  {% if paginator.next_page %}
    <a href="{{ paginator.next_page_path | relative_url }}">Next &raquo;</a>
  {% else %}
    <span>Next &raquo;</span>
  {% endif %}
</div>
{% endif %}