1

I've run into an issue when using Jekyll (which uses the Liquid templating language), the plugin jekyll-paginate-v2 and offset.

The problem: I have a featured section on my blog that always shows the most recent post. The other posts appear below, 10 per page using paginate.

I tried to simply show them using {% for post in paginator.posts offset:1 %}, but this is flawed in two ways:

  • it only shows 9 posts per page instead of 10
  • it offsets the first post on each page, so that the 1st, 10th, 20th, etc. posts are hidden, which is not what I want

What I'm trying to achieve: a loop that always ignores the post at index 0 and shows everything else as it normally would.

Am I just using offset wrong, or is this a bug in jekyll-paginate-v2?

Right now I've "fixed" it by doing the following, but it's not ideal as the first page only shows 9 posts:

<div class="articles-showcase row post-list">
{% assign featuredpost = site.posts.first %}
  {% for post in paginator.posts %}
  {% if post == featuredpost %}
  {% continue %}
  {% else %}
  <!-- Article Card -->
  <div class="article-detail-box col-lg-6 col-md-6 col-sm-12 card-article-div" data-cat="{{post.category | join: ','}}">
    <div class="card card-article box-shadow h-100">
      <a href="{{ site.baseurl }}{{post.url}}" class="card-article-linker" title="{{post.title}}"></a>
      <div class="img-div text-center post-card-header bg-gray">
        <img src="{{ site.baseurl }}{{post.image}}" alt="{{post.title}}">
      </div>
      <div class="card-article-body">
        <p class="author-name">{{post.author}} | {{ post.date | date: '%B %d, %Y' }}</p>
        <a href="{{site.baseurl}}{{post.url}}">
          <h5>{{post.title}}</h5>
        </a>
        <div class="article-description">{{post.content}}</div>
      </div>
    </div>
  </div>
  {% endif %}
  {% endfor %}
</div>
4

3 回答 3

2

I raised this as an issue to the jekyll-paginate-v2 developers, and it has now been solved: https://github.com/sverrirs/jekyll-paginate-v2/issues/100

于 2018-08-30T22:30:25.457 回答
1

As a quick fix you should be able to add a front-matter property to the latest post manually:

---
layout: page
pagination: 
  enabled: false
---

Otherwise, you either have to exclude the paginator-v2 plugin and write your own logic for pagination or else extend the plugin in your _plugins dir so that the posts data sent to the paginate plugin already excludes the first (actually last because the reverse sorting will be done afterwards.) post.

于 2018-07-11T03:14:06.840 回答
0

I've hacked together a flawed solution that I'm using until something better comes along. I'll share it here for now:

   <div class="articles-showcase row post-list">
  {% if paginator.page == 1 %}
    {% for post in site.posts limit:11 %}
    {% if post == featuredpost %}
      {% continue %}
    {% else %}
    <!-- Article Card -->
    <div class="article-detail-box col-lg-6 col-md-6 col-sm-12 card-article-div" data-cat="{{post.category | join: ','}}">
      <div class="card card-article box-shadow h-100">
        <a href="{{ site.baseurl }}{{post.url}}" class="card-article-linker" title="{{post.title}}"></a>
        <div class="img-div text-center post-card-header bg-gray">
          <img src="{{ site.baseurl }}{{post.image}}" alt="{{post.title}}">
        </div>
        <div class="card-article-body">
          <p class="author-name">{{post.author}} | {{ post.date | date: '%B %d, %Y' }}</p>
          <a href="{{site.baseurl}}{{post.url}}">
            <h5>{{post.title}}</h5>
          </a>
          <div class="article-description">{{post.content}}</div>
        </div>
      </div>
    </div>
    {% endif %}
  {% endfor %}
  {% endif %}
  {% if paginator.page > 1 %}
  {% for post in paginator.posts %}
    <!-- Article Card -->
    <div class="article-detail-box col-lg-6 col-md-6 col-sm-12 card-article-div" data-cat="{{post.category | join: ','}}">
        <div class="card card-article box-shadow h-100">
          <a href="{{ site.baseurl }}{{post.url}}" class="card-article-linker" title="{{post.title}}"></a>
          <div class="img-div text-center post-card-header bg-gray">
            <img src="{{ site.baseurl }}{{post.image}}" alt="{{post.title}}">
          </div>
          <div class="card-article-body">
            <p class="author-name">{{post.author}} | {{ post.date | date: '%B %d, %Y' }}</p>
            <a href="{{site.baseurl}}{{post.url}}">
              <h5>{{post.title}}</h5>
            </a>
            <div class="article-description">{{post.content}}</div>
          </div>
        </div>
      </div>
  {% endfor %}
  {% endif %}
</div>

The main problem with it is the 2nd paginated page shows the last article on the 1st page again. For me that's better than only showing 9 posts on the first page, however. Any better solution would be greatly appreciated!

于 2018-07-10T18:37:43.527 回答