2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Craft CMS で前後のエントリを取得する(ストラクチャーの場合)

Last updated at Posted at 2019-04-19

Craft CMS の詳細ページテンプレートで前後のエントリへのリンクを出力する場合、セクションタイプが ストラクチャーチャンネル かで取得方法が異なる。

今回は ストラクチャー の場合のサンプルコードについて。

直前のエントリへのリンクを出力

{% set prevEntry = craft.entries().prevSiblingOf(entry).one() %}

{% if prevEntry %}
  <a href="{{ prevEntry.url }}">{{ prevEntry.title }}</a>
{% endif %}

エントリ詳細ページ向けのテンプレートであれば entry が現在のエントリにあたるため、 .prevSiblingOf(entry) で直前のエントリを取得できる。

該当するエントリが存在しない場合を想定し {% if prevEntry %} も定義する。

直後のエントリへのリンクを出力

{% set nextEntry = craft.entries().nextSiblingOf(entry).one() %}

{% if nextEntry %}
  <a href="{{ nextEntry.url }}">{{ nextEntry.title }}</a>
{% endif %}

直後のエントリの取得は .nextSiblingOf(entry) となる。
セットしている変数名が異なるものの、「直前のエントリ」の例と同じ内容。

参考

Craft CMS のストラクチャーで親子関係のデータを取得する | BUN:Log
https://bunlog.dreamseeker.dev/get-parent-child-relationship-data-with-craftcms

ストラクチャーで親子関係のデータを取得する方法は、こちらが参考になるかと。。。
ただし、Craft CMS 2.x 向けの内容のため、追って最新版を Qiita に投稿する予定。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?