Docs: Other pages and menus
Other pages and menus
Reaching beyond the current page: pages, page, children, siblings, menu and breadcrumbs, with every listing option and error code.
Use these to list content, reach a parent or a neighbour, and draw menus. They never expose a draft and they respect translations. The examples use the $esc shortcut from the overview.
Pages and listings #
pages($query = []) #
Lists pages by Layout, parent, what links to a page, or a field's value, sorted and paged. It returns {items, total, page, pages, limit, error} (E). total counts every match, pages how many pages of limit they fill, and error is null when the query ran.
<?php $list = $this->pages(['linking_to' => 'self', 'page' => $this->query('page', 'int', 1)]); ?>
<?php foreach ($list['items'] as $item): ?><a href="<?= $item['url'] ?>"><?= $item['title'] ?></a><?php endforeach; ?>
<?php if ($list['page'] < $list['pages']): ?><a href="<?= $this->currentUrl(['page' => $list['page'] + 1]) ?>">Older</a><?php endif; ?>
Each item is {id, title, url, description, image, created, updated, template, parent_id, current, fields, option_values, author, sort}. Every string is already escaped, so echo it as it is. current is true for the page being shown. created and updated are Y-m-d H:i:s. Hand an item to picture($item) or image($item) for its image.
fields holds the values you asked for, by field()'s rule: a rich-text or HTML value as stored, a list as escaped strings, a repeater as rows, a gallery as escaped {image, alt} rows.
Options for pages() #
All are optional and they combine with AND.
template: a Layout key, or a list of up to 10.parent: a page id,'self', or0for the top level.children()is this option.linking_to: a page id or'self'. Lists pages whose relation field lists that page, such as a category's archive.via: a relation field key. Withlinking_to, only through that field.exclude: an id,'self', or a list of up to 50.where:['field_key' => value, ...], up to 10. Needstemplate.sort:created(the default),updated,title,id,sort(since 1.20.2, the order the site owner set) orfield.<key>.field.needstemplate. Withsort, pages holding a value come first, smallest first, and the rest follow newest first.order:ascordesc. The default is newest first for dates and ids, A to Z for titles and fields.limit: 1 to 100, default 10. A larger number is cut to 100.page: 1 and up. Read it with$this->query('page', 'int', 1), never$_GET. Whereoffsetplus the pages before this one pass 10,000 results,erroris'page'.offset: 0 and up. Skips that many first, for a featured item shown above.fields: a list of up to 20 field keys, returned in each item'sfields.
'self' is the page being shown. where and sort => 'field.<key>' take text, textarea, number, select, toggle, date, URL, email and colour fields. A number compares by value (9 before 10), a toggle on or off, the rest exactly, and an empty value sorts last both ways. fields cannot ask for a relation (pass its ids to linking_to instead) or a message.
A field sort, a where, and a title sort in an extra language read up to 5,000 matching pages. Narrow the query by template, parent or linking_to if you have more. They read every candidate on each request the page cache does not serve, so keep them narrow on busy pages. A field its condition hides on a page counts as that page not having it.
Since 1.20.2: sort is the page's Sort value as a number, 0 when it holds none. author is the page author's display name, as on posts(), never their handle. option_values has an entry for each select or multiselect you asked for in fields: the value beside the title, escaped. A relation in fields gives the ids of the pages this visitor may see, in the stored order, in one query however long the list is; a relation to posts is refused, so use related() for those.
What pages() leaves out, and what it translates #
Only published pages are listed. A draft never appears, not even its title. The one exception is the page a reviewer is previewing, which is listed where it belongs. Since 1.20.2 a page hidden from lists is left out of every list, menu, search, feed, sitemap and total, while page() and a parent or linking_to naming it still answer. On a page in an extra language, titles, descriptions and text fields come translated, URLs carry the prefix, and a title sort follows the translated titles.
Error codes #
Bad input lists nothing and error names the problem. The option at fault is one of template, parent, linking_to, via, exclude, where, sort, order, limit, page, offset or fields. parent is also the code when that parent is a draft or does not exist, so a draft never confirms that it has children. The other codes:
query: the query was not an array.option: a key that is not in the list above. Check its spelling.self:'self'where no page is shown, such as on the Blog or a 404.tree:parentwas used before the update's migration ran.scan: more than 5,000 pages to sort or filter.db: the database failed.
Nothing is thrown, so a typo never breaks a public page. Print $list['error'] while you build.
Three recipes #
<?php // Newest posts
$this->pages(['template' => 'post', 'limit' => 5, 'exclude' => 'self']);
// This page's children, A to Z
$this->pages(['parent' => 'self', 'sort' => 'title', 'limit' => 100]);
// A category's archive, paged
$this->pages(['linking_to' => 'self', 'page' => $this->query('page', 'int', 1)]); ?>
page($ref = 'self') #
One page as a pages() item (E, with fields empty), or null. $ref is 'self', an id, or a path such as docs/install. A path is as the page tree stores it: a slash at either end is ignored, there is no language prefix, and a digit string is a path. A draft is null, by id and by path alike, so neither confirms that it exists. The one exception is the page an authorised preview shows.
An item's parent_id names its parent. A top-level page has parent_id 0, which gives null.
<?php $parent = $this->page($this->page()['parent_id']); ?>
<?php if ($parent): ?><a href="<?= $parent['url'] ?>"><?= $parent['title'] ?></a><?php endif; ?>
children($ref = 'self', $query = []) #
The pages under $ref, in the pages() envelope. It is pages(['parent' => $ref] + $query), so every option and error code above applies, and 0 is the top level. A parent the visitor may not see lists nothing, with error: 'parent'.
<?php foreach ($this->children('self', ['sort' => 'title'])['items'] as $c): ?><a href="<?= $c['url'] ?>"><?= $c['title'] ?></a><?php endforeach; ?>
siblings($ref = 'self', $query = []) #
The pages beside $ref under the same parent, $ref included. current marks the page being shown, so the item after it is the next link. Of a page whose parent is unpublished it lists nothing, with error: 'parent'. Both children() and siblings() are paged like pages() (10 by default), so ask for enough to hold every sibling.
<?php $list = $this->siblings('self', ['limit' => 100])['items']; $at = array_search(true, array_column($list, 'current'), true); ?>
<?php if ($at !== false && isset($list[$at + 1])): ?><a href="<?= $list[$at + 1]['url'] ?>"><?= $list[$at + 1]['title'] ?></a><?php endif; ?>
A page's settings #
New in 1.19.0. The site owner can switch on four things under Pages ▸ Settings, all off until then: a breadcrumb trail, a "last updated" line, a list of the child pages and anchors on headings. Core draws the first three in a CMS page itself, in the components page-breadcrumbs, page-updated and page-children, which you can place with component() or restyle by shipping a file of the same name. Calling pageTrail(), pageUpdated() or pageChildren() claims that part, so core draws no second copy. The fourth needs nothing from you: core gives each heading from h2 to h4 in the page's content an id and a heading-anchor link; style .heading-anchor inside your prose. A Blog post's body gets the same anchors while the Blog's "Links on headings" setting is on, so the same style covers both. Reactions, added in 1.20.0, work differently: see pageReactionsBlock() below.
pageSettings() #
The six switches, each a boolean: breadcrumbs, child_list, last_updated, heading_anchors, since 1.20.0 reactions and, since 1.21.0, share. The first four are off until the site owner turns them on, and so is share. reactions is on, but each page also has its own Reactions switch, so a page takes reactions only while that is on too. Each page has its own Share switch as well, on for a new page, so a page shows the share buttons only while both are on.
pageTrail() #
What breadcrumbs() gives (P) while breadcrumbs is on and the page sits under another page; otherwise [].
pageChildren() #
The published pages under this one, as children('self') items sorted by title, at most 100 (E), while child_list is on; otherwise [].
pageUpdated() #
When the page last changed, as a Y-m-d H:i:s UTC string for date() (P), while last_updated is on; otherwise an empty string.
<?php $updated = $this->pageUpdated(); ?>
<?php if ($updated !== ''): ?><p><?= $this->timeTag($updated) ?></p><?php endif; ?>
pageReactions() #
New in 1.20.0. The page's reaction row for this visitor, {kind, item, counts, mine, may, set, any}, to hand as it is to core's reactions component. It is null in a preview, on a draft, and while reactions are off and nobody has reacted. Reactions left before they were turned off stay in the row, with may false. Since 1.21.0 any says the picker offers every emoji; set then holds only the quick picks.
pageReactionsBlock() #
New in 1.20.0. Prints that row with the reactions component, and nothing when the row is null. Under View as the chips show but cannot be pressed. Core does not draw this for you. The default theme's views/dynamic-page.php calls it after the page's content, so a theme with its own views/dynamic-page.php shows the bar only once it calls it too.
<?php $this->pageReactionsBlock(); ?>
pageShare() #
New in 1.21.0. What the share component draws for this page: {title, url, path, links, script}. It is null unless the share switch and the page's own Share switch are both on and the page is published. It is also null on a draft or proposal preview, in a theme preview and under View as. url is built from the site address the site owner configured. Calling it claims the part; a theme that never calls it gets core's page-share component, drawn at the end of the page.
pagePolicy() #
New in 1.21.0. The sections of the privacy page, as a list of {id, heading, body, off, link, link_text, cleared} (P, so escape what you print). It is null on every other page, and on the privacy page while nothing is on (today: online status off). off holds :link, or is empty while members may not hide their status. Core draws it with the page-policy component, and the online status section has the id online-status.
Menus and the breadcrumb trail #
menu($identifier) #
A menu as a tree of {title, url, target, class, active, heading, change, children} (P). It gives [] when no menu has that identifier. class is new in 1.17.0.
<?php $h = static fn ($v): string => htmlspecialchars((string) $v, ENT_QUOTES, 'UTF-8'); ?>
<?php foreach ($this->menu('main_nav') as $node):
$classes = (!empty($node['active']) ? ' active' : '') . ($node['class'] !== '' ? ' ' . $h($node['class']) : '');
$change = isset($node['change']) ? ' data-draft-change="' . $h($node['change']) . '"' : '';
$target = $node['target'] === '_blank' ? ' target="_blank" rel="noopener noreferrer"' : ''; ?>
<?php if ($node['heading']): ?><span class="nav-item<?= $classes ?>"<?= $change ?>><?= $h($node['title']) ?></span>
<?php else: ?><a href="<?= $h($node['url']) ?>" class="nav-item<?= $classes ?>"<?= $target . $change ?>><?= $h($node['title']) ?></a><?php endif; ?>
<?php endforeach; ?>
menu()reads the menu, never the database. Links to unpublished or deleted pages are already left out (a heading when they have children), a link to the page the site shows at/is already/, an unsafe scheme is already#, and a heading'surlis an empty string.targetis_selfor_blank. Addrel="noopener noreferrer"beside_blank.classis the CSS class names the site owner gave the item, or an empty string. Add them to the item'sclassattribute, escaped like everything else.changeisaddedorchangedonly while a reviewer previews a menu draft, otherwisenull. Print it asdata-draft-changewhen it is notnull.- The page the site shows at
/is the one namedhome, unless the site owner chose another page or a module's landing (since 1.20.0). The page namedhomeis then an ordinary page at/home. - A custom link saved at a section's former address is drawn at the section's current address, in every language, and
activefollows the address drawn. The exception is a link the site's own redirect rule sends somewhere else first. - On a translated render the titles are translated and links to pages carry the prefix. Since 1.20.0 a custom link carries it too when it is
/, a published page's address, or the current address of a section that renders in languages. Any other custom link stays as stored. - The default theme builds its home page buttons from a menu (
home_actions), so a fresh install shows no button pointing nowhere.
breadcrumbs() #
The trail to this page as {title, url, current} (P): Home, each ancestor, then the page. current is true on the page itself. In a language the titles are translated and the URLs carry the prefix. A draft ancestor is left out. Home is the title of the published page the site shows at /, else the site name, which is also what you get when the site owner chose a module's landing as the home page. The home page gives Home alone, and a route that is not a CMS page (the Blog, a 404) gives [], so render nothing then. You need not add structured data: for a page below the top level, core's SEO block already emits a BreadcrumbList with the same trail.
<?php foreach ($this->breadcrumbs() as $c): ?><a href="<?= $esc($c['url']) ?>"><?= $esc($c['title']) ?></a><?php endforeach; ?>