【问题标题】:Jekyll exclude tag from paginationJekyll 从分页中排除标签
【发布时间】:2023-03-16 18:07:01
【问题描述】:

我有一些带有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 个块,然后我的代码过滤它们 - 我想在计算分页之前应用过滤。

【问题讨论】:

标签: jekyll liquid github-pages


【解决方案1】:

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

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

第一页和第二页的分页仍然正确。

【讨论】:

    【解决方案2】:

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

    1. Gemfile中删除gem jekyll-paginate

    2. _config.yml中设置需要的配置变量:

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

    4. 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
      
    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>
      
    6. 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。

    【讨论】:

    • 我不认为这可以处理我正在寻找的分页。
    • 不,这并不是要处理分页。据我了解,它说要合并第一页中的非 foo 帖子和第 2 页中的 foo 帖子。
    • 您误读了@marcanuy 的问题。我不希望 foo 帖子显示在任何地方,但我希望分页保持不间断。
    • 更新了包含foo标签的答案过滤帖子。
    【解决方案3】:

    [更新答案]

    解决方案 1. 新系列

    您报告分页器对象上的where 命令不起作用/重新计算分页。因此,我建议将所有“foo”帖子保存在单独的集合中。这解决了您最初的问题,但如果您想在另一个页面上显示所有帖子组合,则会产生一个新问题。如果你不想这样,这是最优雅的解决方案。

    解决方案 2. 使用 Javascript 进行分页

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

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

    【讨论】:

    • 感谢@JoostS,但这并不能完全解决问题。虽然这会从页面中删除页面,但不会重新计算分页。
    猜你喜欢
    • 2014-10-16
    • 2021-10-21
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    相关资源
    最近更新 更多