Skip to content

Commit

Permalink
fix: pagination error when pinned posts exceed the page size (#1965)
Browse files Browse the repository at this point in the history
  • Loading branch information
cotes2020 authored Sep 26, 2024
1 parent e6b87d2 commit 93f616b
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions _layouts/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,45 @@

{% include lang.html %}

{% assign pinned = site.posts | where: 'pin', 'true' %}
{% assign default = site.posts | where_exp: 'item', 'item.pin != true and item.hidden != true' %}
{% assign all_pinned = site.posts | where: 'pin', 'true' %}
{% assign all_normal = site.posts | where_exp: 'item', 'item.pin != true and item.hidden != true' %}

{% assign posts = '' | split: '' %}

<!-- Get pinned posts -->
<!-- Get pinned posts on current page -->

{% assign offset = paginator.page | minus: 1 | times: paginator.per_page %}
{% assign pinned_num = pinned.size | minus: offset %}
{% assign visible_start = paginator.page | minus: 1 | times: paginator.per_page %}
{% assign visible_end = visible_start | plus: paginator.per_page %}

{% if pinned_num > 0 %}
{% for i in (offset..pinned.size) limit: pinned_num %}
{% assign posts = posts | push: pinned[i] %}
{% if all_pinned.size > visible_start %}
{% if all_pinned.size > visible_end %}
{% assign pinned_size = paginator.per_page %}
{% else %}
{% assign pinned_size = all_pinned.size | minus: visible_start %}
{% endif %}

{% for i in (visible_start..all_pinned.size) limit: pinned_size %}
{% assign posts = posts | push: all_pinned[i] %}
{% endfor %}
{% else %}
{% assign pinned_num = 0 %}
{% assign pinned_size = 0 %}
{% endif %}

<!-- Get default posts -->
<!-- Get normal posts on current page -->

{% assign default_beg = offset | minus: pinned.size %}
{% assign normal_size = paginator.posts | size | minus: pinned_size %}

{% if default_beg < 0 %}
{% assign default_beg = 0 %}
{% endif %}
{% if normal_size > 0 %}
{% if pinned_size > 0 %}
{% assign normal_start = 0 %}
{% else %}
{% assign normal_start = visible_start | minus: all_pinned.size %}
{% endif %}

{% assign default_num = paginator.posts | size | minus: pinned_num %}
{% assign default_end = default_beg | plus: default_num | minus: 1 %}
{% assign normal_end = normal_start | plus: normal_size | minus: 1 %}

{% if default_num > 0 %}
{% for i in (default_beg..default_end) %}
{% assign posts = posts | push: default[i] %}
{% for i in (normal_start..normal_end) %}
{% assign posts = posts | push: all_normal[i] %}
{% endfor %}
{% endif %}

Expand Down

0 comments on commit 93f616b

Please sign in to comment.