Jekyll 从分页中排除标签

Jekyll exclude tag from pagination

我有一些贴有 foo 标签的帖子,我想将它们从我的首页中排除。

我试过将此代码放入首页模板中:

<div class="posts">
  {% for post in paginator.posts %}
  {% unless post.tags and post.tags contains "foo" %}

  {% endunless %}
  {% endfor %}
</div>

然而,这会导致分页不正确。

以下是一些示例帖子:

+-------+--------+-----+
| Index |  Post  | Tag |
+-------+--------+-----+
|     1 | Red    | foo |
|     2 | Blue   |     |
|     3 | White  |     |
|     4 | Pink   | foo |
|     5 | Orange |     |
|     6 | Yellow | foo |
|     7 | Beige  | foo |
|     8 | Purple |     |
|     9 | Black  | foo |
+-------+--------+-----+

实际输出:

  1. 第 1 页:2, 3, 5
  2. 第 2 页:8

我想要什么:

  1. 第 1 页:2, 3, 5, 8

如您所见,它目前正在将帖子分成 5 个块,然后 然后 我的代码会过滤它们 - 我想在计算分页之前应用过滤。

如果不破解分页器插件就无法完成,所以我们开始吧:

  1. Gemfile

  2. 中删除 gem jekyll-paginate
  3. _config.yml中设置需要的配置变量:

     paginate: 2
     paginate_path: "/blog/page:num/"
    
  4. 创建 _plugins 目录

  5. pager.rbpagination.rb复制到_plugins/

     cd _plugins
     wget https://github.com/jekyll/jekyll-paginate/blob/master/lib/jekyll-paginate/pager.rb
     wget https://github.com/jekyll/jekyll-paginate/blob/master/lib/jekyll-paginate/pagination.rb
    
  6. 在主页中显示带有文档中使用的建议代码的帖子

    <!-- 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 %}
    
    <h1>  Paginator</h1>
    <!-- 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>
    
  7. pagination.rb中修改paginate函数来过滤包含标签foo的帖子,目前all_posts变量包含所有帖子数据用于计算分页,所以我们需要删除包含标签的那些:

           all_posts = all_posts.select do |elem|
             !elem.data['tags'].include? 'foo'
           end
    

    那么该函数将如下所示:

         def paginate(site, page)
           all_posts = site.site_payload['site']['posts'].reject { |post| post['hidden'] }
           all_posts = all_posts.select do |elem|
             !elem.data['tags'].include? 'foo'
           end
           pages = Pager.calculate_pages(all_posts, site.config['paginate'].to_i)
           (1..pages).each do |num_page|
             pager = Pager.new(site, num_page, all_posts, pages)
             if num_page > 1
               newpage = Page.new(site, site.source, page.dir, page.name)
               newpage.pager = pager
               newpage.dir = Pager.paginate_path(site, num_page)
               site.pages << newpage
             else
               page.pager = pager
             end
           end
         end
    

然后第 1 页将显示所需的帖子,仅显示不包含 foo 标签的帖子:2、3、5、8。

[更新答案]

解决方案1.新建collection

您报告说分页器 object 上的 where 命令没有 work/recalculate 分页。因此,我建议将所有 'foo' 个帖子保存在单独的 collection 中。这解决了您原来的问题,但如果您想在另一页上显示所有合并的帖子,则会产生新问题。如果你不想那样,这是最优雅的解决方案。

方案2.使用Javascript进行分页

忘记分页器 object 并通过在 Javascript 中构建您自己的分页(或无限滚动)来解决这个问题,因为无论如何 pagination is a SEO issue...

注意:我已经为 Jekyll without plugins 创建了一个资源。我也会在这里添加Javascript分页。

只需在 yaml frontmatter 中添加 hidden: true 变量,帖子就不会出现在首页分页中。参见 this

虽然这不会按标签过滤(您需要一个接一个地浏览帖子并手动添加选项),但它似乎比上面建议的调整要简单得多。

第一页和第二页的分页保持正确。