Skip to main content Link Search Menu Expand Document (external link)

Custom - Phân trang danh sách bài viết

  • Thêm code sau vào file '_functions'
{% macro paging(trang,p,max,b) %}
{% spaceless %}
  {% if max > 1 %}
    <center><div class="pagination">
      {% set a=' <a class="pagenav" href="/'~trang~'' %}
      {% if p>max %}{% set p=max %}a{% endif %}
      {% if p>1 %}
        {{a|raw}}{{p-1}}{{b}}">«</a>
      {% endif %}
      {% if p>3 %}
        {{a|raw}}1{{b}}">1</a>
      {% endif %}
      {% if p>4 %}
         <span>...</span>
      {% endif %}
      {% if p>2 %}
        {{a|raw}}{{p-2}}{{b}}">{{p-2}}</a>
      {% endif %}
      {% if p>1 %}
        {{a|raw}}{{p-1}}{{b}}">{{p-1}}</a>
      {% endif %}
      <span class="currentpage"><b>{{p}}</b></span>
      {% if p<max-1 %}
        {{a|raw}}{{p+1}}{{b}}">{{p+1}}</a>
      {% endif %}
      {% if p<max-2 %}
        {{a|raw}}{{p+2}}{{b}}">{{p+2}}</a>
      {% endif %}
      {% if p<max-3 %}
        <span>...</span>
      {% endif %}
      {% if p<max %}
        {{a|raw}}{{max}}{{b}}" class="next">{{max}}</a>
      {% endif %}
      {% if p<max %}
        {{a|raw}}{{p+1}}{{b}}">»</a>
      {% endif %}
    </div></center>
  {% endif %}
{% endspaceless %}
{% endmacro %}

CSS

.pagination{text-align:center}.currentpage{background-color:#4E342E;padding:3px 11px;margin:2px;border-radius:50px;color:#fff!important;box-shadow:0 0 7px rgba(0,0,0,0.2),0 0 0 1px rgba(188,188,188,0.1)}.pagenav,a.pagenav{background-color:#00B7FF;color:#fff!important;text-decoration:none!important;padding:3px 11px;margin:2px;border-radius:50px;box-shadow:0 0 7px rgba(0,0,0,0.2),0 0 0 1px rgba(188,188,188,0.1);animation:namedcolors 3s infinite alternate}.pagevav:hover,a.pagenav:hover{background-color:#54a3c6;color:#fff!important}
  • Cách sử dụng:
{% from '_functions' import paging %}

{% set total = count(query_select_table(table_name)) %}
{% set per = '10' %}
{% set page_max = total//per %}
{% if total//per != total/per %}{% set page_max = total//per+1 %}{% endif %}
{% set p = GET['page']|default(1) %}
{% if p matches '/[a-zA-z]|%/' or p < 1 %}{% set p = 1 %}{% endif %}
{% if p > page_max %}{% set p = page_max %}{% endif %}
{% set st=p*per-per %}

{% if total_blog == 0 %}
  <p class="highlight">Chưa có bài viết nào!</p>
{% else %}
  {% set list = query_select_table(table_name,'*','ORDER BY id DESC LIMIT '~st~','~per) %}
  {% for data in list %}
    {{data.column}}
  {% endfor %}
{% endif %}
<p>{{paging('?page=',p,page_max)|replace({'/=':'/','/?':'?'})|raw}}</p>

Chia sẻ



QRcode
QRcode