Jekyll excludes tag from pagination

I have posts with tags fooand I want to exclude them from my first page.

I tried putting this code in the first page template:

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

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

However, this leads to incorrect page breaks.

Here are some sample messages:

+-------+--------+-----+
| 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 |
+-------+--------+-----+

Actual conclusion:

  • Page 1: 2, 3, 5
  • Page 2: 8

What I would like:

  • Page 1: 2, 3, 5, 8

As you can see, the column is currently split into 5 and then my code filters them - I would like to apply filtering before pagination is calculated.

+4
source share
3 answers

This cannot be done without hacking the paginator plugin, so we go:

  • remove gem jekyll-paginatefromGemfile

  • _config.yml:

     paginate: 2
     paginate_path: "/blog/page:num/"
    
  • _plugins

  • 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
    
  • ,

    <!-- 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>
    
  • 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.

+2

hidden: true yaml frontmatter, . . this.

( ), , .

.

+2

[ ]

1.

, where paginator / . "foo" . , , , . , .

2. Javascript

paginator , ( infinte scroll) Javascript, pagination - SEO ...

Note. I created a resource for Jekyll without plugins . I will also add Javascript fragmentation here.

0
source

Source: https://habr.com/ru/post/1687439/


All Articles