Help us understand the problem. What is going on with this article?

【CraftCMS】エントリ一覧のページネーション

More than 1 year has passed since last update.

忘れないうちに今回使った小技を出来るだけ残す。

CMS開発で誰もが苦しむページネーションはCraftCMSなら瞬殺出来ます。

登録日付降順で30件ごとにページネーションしましょうね〜

{% paginate craft.entries.section("ahiru").orderBy("postDate DESC").limit(30) as pageInfo,pageEntries %}

{# 1エントリずつ出力 #}
{% if pageEntries | length > 0 %}
    <ul>
    {% for detail in pageEntries %}
        <li>{{detail.title}}</li>
    {% endfor %}
    </ul>
{% endif%}

ページネーションリンクはこう書きます
全部出力するならloop.indexとか当てたらfor文一発で出来るかなと

<a href="{{pageInfo.getPageUrl(1)}}"> 1 </a> 
<a href="{{pageInfo.getPageUrl(2)}}"> 2 </a> 
<a href="{{pageInfo.getPageUrl(3)}}"> 3 </a> 
<a href="{{pageInfo.getPageUrl(4)}}"> 4 </a> 

現在が何ページ目である情報、何ページ目はあるかどうか等を加味すると
前後にこんな感じで書きます

{% if pageInfo.getPageUrl(1) %}{% if pageInfo.currentPage == 1 %} 1 {% else %}<a href="{{pageInfo.getPageUrl(1)}}"> 1 </a>{% endif %}{% endif %}

現在が何ページ目か・・・・pageInfo.currentPage
nページ目は存在するか・・pageInfo.getpageUrl(n)

今回はMAX8ページだったのでベタ書きしたけどループでどうにでも行ける書き方もいけますね
(たぶんだけど)

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした