【问题标题】:October CMS limit pages in pagination十月 CMS 限制分页页面
【发布时间】:2022-01-13 08:17:49
【问题描述】:

我想问一下如何限制页面,例如,如果我只想要分页中的 4 页,然后是箭头。现在它显示了所有页面:

enter image description here

这是我现在使用的代码:

{% if posts.lastPage > 1 %}
  <div class="pagination">
      <ul>
          {% if posts.currentPage > 1 %}
              <li><a class="pagination--left-arrow"href="{{ this.page.baseFileName|page({ (pageParam): (posts.currentPage-1) }) }}">&larr;</a></li>
          {% endif %}

          {% for page in 1..posts.lastPage %}
              <li class="{{ posts.currentPage == page ? 'active' : null }}">
                  <a href="{{ this.page.baseFileName|page({ (pageParam): page }) }}">{{ page }}</a>
              </li>
          {% endfor %}

          {% if posts.lastPage > posts.currentPage %}
              <li><a class="pagination--right-arrow"href="{{ this.page.baseFileName|page({ (pageParam): (posts.currentPage+1) }) }}">&rarr;</a></li>
          {% endif %}
      </ul>
  </div>
{% endif %}

【问题讨论】:

  • 你可以添加像{% set toPage = posts.currentPage + 3 %} {% for page in posts.currentPage..toPage %}这样的中间循环..并为currentPage添加默认值意味着如果它没有设置为1..左右
  • 并调整变量,使the current page 按钮停留在中间而不是第一个可能...

标签: php content-management-system octobercms


【解决方案1】:

这是我的代码。我希望它能拥有你。它的工作,但它需要优化,我没有更多的时间,所以你可以更新它。

流程:

  •     {% if lastPage <= 5 %}
            {% for item in 1..lastPage %}
                {% if currentPage == item %}
                    <li><span>{{ item }}</span></li>
                {% else %}
                    <li><a href="{{ paginator.url(item) }}">{{ item }}</a></li>
                {% endif %}
            {% endfor %}
        {% else %}
            {% if currentPage >= 1 and currentPage < 4 %}
                {% for item in 1..4 %}
                    {% if currentPage == item %}
                        <li><span>{{ item }}</span></li>
                    {% else %}
                        <li><a href="{{ paginator.url(item) }}">{{ item }}</a></li>
                    {% endif %}
                {% endfor %}
                <li  class="disabled not-active"><a href="#">...</a></li>
                {% for item in (lastPage-2)..lastPage %}
                    {% if currentPage == item %}
                        <li><span>{{ item }}</span></li>
                    {% else %}
                        <li><a href="{{ paginator.url(item) }}">{{ item }}</a></li>
                    {% endif %}
                {% endfor %}  
            {% elseif currentPage >= 4 and currentPage < lastPage - 1  %}
                {% if currentPage == firstPage %}
                    <li><span>{{ firstPage }}</span></li>
                {% else %}
                    <li><a href="{{ paginator.url(firstPage) }}">{{ firstPage }}</a></li>
                {% endif %}
                <li  class="disabled"><a href="#">...</a></li>
                {% for item in currentPage-1..currentPage+1 %}
                    {% if currentPage == item %}
                        <li><span>{{ item }}</span></li>
                    {% else %}
                        <li><a href="{{ paginator.url(item) }}">{{ item }}</a></li>
                    {% endif %}
                {% endfor %}
                <li  class="disabled not-active"><a href="#">...</a></li>
                {% if currentPage == lastPage %}
                    <li><span>{{ lastPage }}</span></li>
                {% else %}
                    <li><a href="{{ paginator.url(lastPage) }}">{{ lastPage }}</a></li>
                {% endif %}
            {% else %}
                {% if currentPage == firstPage %}
                    <li><span>{{ firstPage }}</span></li>
                {% else %}
                    <li><a href="{{ paginator.url(firstPage) }}">{{ firstPage }}</a></li>
                {% endif %}
                <li  class="disabled not-active"><a href="#">...</a></li>
                {% for item in lastPage-2..lastPage%}
                    {% if currentPage == item %}
                        <li><span>{{ item }}</span></li>
                    {% else %}
                        <li><a href="{{ paginator.url(item) }}">{{ item }}</a></li>
                    {% endif %}
                {% endfor %}
            {% endif %}
    
        {% endif %}
    
        <li><a href="{{ hasMorePages ? nextPageUrl : '#' }}" class="{{ hasMorePages ? '' : 'not-active' }}"><i class="fa fa-angle-double-right"></i></a></li>
    </ul>
    
  • 【讨论】:

      【解决方案2】:

      也许可以使用|slice

      示例

      {% for page in posts|slice(posts.currentPage, 4) %}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-18
        • 1970-01-01
        • 2019-01-26
        相关资源
        最近更新 更多