Jekyll 从分页中排除标签

Jam*_*ger 5 liquid jekyll github-pages

我有一些带有 标签的帖子foo,我想从我的首页中排除这些帖子。

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

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

  {% endunless %}
  {% endfor %}
</div>
Run Code Online (Sandbox Code Playgroud)

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

以下是一些示例帖子:

+-------+--------+-----+
| 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 |
+-------+--------+-----+
Run Code Online (Sandbox Code Playgroud)

实际输出:

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

我想要什么:

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

正如您所看到的,它当前将帖子分成 5 个块,然后我的代码对它们进行过滤 - 我想在计算分页之前应用过滤。

mar*_*nuy 2

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

  1. gem jekyll-paginate从......中去除Gemfile

  2. 在以下位置设置所需的配置变量_config.yml:

     paginate: 2
     paginate_path: "/blog/page:num/"
    
    Run Code Online (Sandbox Code Playgroud)
  3. 创建_plugins目录

  4. 复制pager.rb并pagination.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
    
    Run Code Online (Sandbox Code Playgroud)
  5. 在主页中显示帖子以及文档中使用的建议代码

    <!-- 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>
    
    Run Code Online (Sandbox Code Playgroud)
  6. 在pagination.rb修改paginate函数来过滤包含 tag 的帖子时foo,当前all_posts变量包含用于计算分页的所有帖子数据,因此我们需要删除包含 tag 的帖子数据:

           all_posts = all_posts.select do |elem|
             !elem.data['tags'].include? 'foo'
           end
    
    Run Code Online (Sandbox Code Playgroud)

    那么该函数将如下所示:

         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
    
    Run Code Online (Sandbox Code Playgroud)

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