# Pages

A pages select field that allows to select one or multiple related pages

****

- Read more: <https://getkirby.com/docs/guide/blueprints/fields>
- Source: <https://github.com/getkirby/kirby/tree/5.5.3/config/fields/pages.php>

****

## What is the pages field?

The pages field is basically a select/multiselect field specialized to allow you to select one or more pages out of a given set of options. A reference to the selected pages(s) is stored in the content file of the respective model (page, user, file). It has a handy navigator to go through the entire site tree and select the pages you want.

Usage examples: Select related pages of the current page to show in a related pages widget on the frontend, select one or more articles to feature on the homepage, etc.

<figure class="image"><a data-lightbox href="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/pages/c67c45066a-1717846665/pages.png?width=1416&amp;height=480&amp;enlarge=0"><img alt="" class="rounded" height="313" loading="lazy" sizes="auto" src="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/pages/c67c45066a-1717846665/pages.png?width=924&amp;height=313&amp;enlarge=0" srcset="https://assets.getkirby.com/media/pages/docs/reference/panel/fields/pages/c67c45066a-1717846665/pages.png?width=462&amp;height=157&amp;enlarge=0 462w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/pages/c67c45066a-1717846665/pages.png?width=690&amp;height=234&amp;enlarge=0 690w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/pages/c67c45066a-1717846665/pages.png?width=924&amp;height=313&amp;enlarge=0 924w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/pages/c67c45066a-1717846665/pages.png?width=1380&amp;height=468&amp;enlarge=0 1380w, https://assets.getkirby.com/media/pages/docs/reference/panel/fields/pages/c67c45066a-1717846665/pages.png?width=1416&amp;height=480&amp;enlarge=0 1848w" width="924"></a></figure>

## Basic field definition

```yaml
fields:
  related:
    label: Related Pages
    type: pages
```

## Field properties



<div class="table">
	<table class="parameters">
		<thead>
			<tr>
				<th>Name</th>
				<th>Type</th>
				<th>Default</th>

								<th>Description</th>
							</tr>
		</thead>
		<tbody>
						<tr>
				<td>
					default				</td>
				<td><code class="type type-mixed">mixed</code></td>

				<td data-label="Default:">
															<code>null</code>
									</td>

								<td>Default selected page(s) when a new page/file/user is created</td>
							</tr>
						<tr>
				<td>
					disabled				</td>
				<td><code class="type type-bool">bool</code></td>

				<td data-label="Default:">
															<code>null</code>
									</td>

								<td>If <code class="type type-bool">true</code>, the field is no longer editable and will not be saved</td>
							</tr>
						<tr>
				<td>
					empty				</td>
				<td><code class="type type-mixed">mixed</code></td>

				<td data-label="Default:">
															<code>null</code>
									</td>

								<td>The placeholder text if none have been selected yet</td>
							</tr>
						<tr>
				<td>
					help				</td>
				<td><code class="type type-mixed">mixed</code></td>

				<td data-label="Default:">
															<code>null</code>
									</td>

								<td>Optional help text below the field</td>
							</tr>
						<tr>
				<td>
					image				</td>
				<td><code class="type type-mixed">mixed</code></td>

				<td data-label="Default:">
															<code>null</code>
									</td>

								<td>Image settings for each item</td>
							</tr>
						<tr>
				<td>
					info				</td>
				<td><code class="type type-string">string</code></td>

				<td data-label="Default:">
															<code>null</code>
									</td>

								<td>Info text for each item</td>
							</tr>
						<tr>
				<td>
					label				</td>
				<td><code class="type type-mixed">mixed</code></td>

				<td data-label="Default:">
															<code>null</code>
									</td>

								<td>The field label can be set as string or associative array with translations</td>
							</tr>
						<tr>
				<td>
					layout				</td>
				<td><code class="type type-string">string</code></td>

				<td data-label="Default:">
															<code>'list'</code>
									</td>

								<td>Changes the layout of the selected entries. Available layouts: <code class="type">list</code>, <code class="type">cardlets</code>, <code class="type">cards</code></td>
							</tr>
						<tr>
				<td>
					link				</td>
				<td><code class="type type-bool">bool</code></td>

				<td data-label="Default:">
															<code>true</code>
									</td>

								<td>Whether each item should be clickable</td>
							</tr>
						<tr>
				<td>
					max				</td>
				<td><code class="type type-int">int</code></td>

				<td data-label="Default:">
															<code>null</code>
									</td>

								<td>The maximum number of allowed selected</td>
							</tr>
						<tr>
				<td>
					min				</td>
				<td><code class="type type-int">int</code></td>

				<td data-label="Default:">
															<code>null</code>
									</td>

								<td>The minimum number of required selected</td>
							</tr>
						<tr>
				<td>
					multiple				</td>
				<td><code class="type type-bool">bool</code></td>

				<td data-label="Default:">
															<code>true</code>
									</td>

								<td>If <code class="type type-bool">false</code>, only a single one can be selected</td>
							</tr>
						<tr>
				<td>
					query				</td>
				<td><code class="type type-string">string</code></td>

				<td data-label="Default:">
															<code>null</code>
									</td>

								<td>Optional query to select a specific set of pages</td>
							</tr>
						<tr>
				<td>
					required				</td>
				<td><code class="type type-bool">bool</code></td>

				<td data-label="Default:">
															<code>null</code>
									</td>

								<td>If <code class="type type-bool">true</code>, the field has to be filled in correctly to be saved.</td>
							</tr>
						<tr>
				<td>
					search				</td>
				<td><code class="type type-bool">bool</code></td>

				<td data-label="Default:">
															<code>true</code>
									</td>

								<td>Enable/disable the search field in the picker</td>
							</tr>
						<tr>
				<td>
					size				</td>
				<td><code class="type type-string">string</code></td>

				<td data-label="Default:">
															<code>'auto'</code>
									</td>

								<td>Layout size for cards: <code class="type">tiny</code>, <code class="type">small</code>, <code class="type">medium</code>, <code class="type">large</code>, <code class="type">huge</code>, <code class="type">full</code></td>
							</tr>
						<tr>
				<td>
					store				</td>
				<td><code class="type type-string">string</code></td>

				<td data-label="Default:">
															<code>'uuid'</code>
									</td>

								<td>Whether to store UUID or ID in the content file of the model</td>
							</tr>
						<tr>
				<td>
					subpages				</td>
				<td><code class="type type-bool">bool</code></td>

				<td data-label="Default:">
															<code>true</code>
									</td>

								<td>Optionally include subpages of pages</td>
							</tr>
						<tr>
				<td>
					text				</td>
				<td><code class="type type-string">string</code></td>

				<td data-label="Default:">
															<code>null</code>
									</td>

								<td>Main text for each item</td>
							</tr>
						<tr>
				<td>
					translate				</td>
				<td><code class="type type-bool">bool</code></td>

				<td data-label="Default:">
															<code>true</code>
									</td>

								<td>If <code class="type type-bool">false</code>, the field will be disabled in non-default languages and cannot be translated. This is only relevant in multi-language setups.</td>
							</tr>
						<tr>
				<td>
					when				</td>
				<td><code class="type type-mixed">mixed</code></td>

				<td data-label="Default:">
															<code>null</code>
									</td>

								<td>Conditions when the field will be shown (since 3.1.0)</td>
							</tr>
						<tr>
				<td>
					width				</td>
				<td><code class="type type-string">string</code></td>

				<td data-label="Default:">
															<code>'1/1'</code>
									</td>

								<td>The width of the field in the field grid, e.g. <code class="type">1/1</code>, <code class="type">1/2</code>, <code class="type">1/3</code>, <code class="type">1/4</code>, <code class="type">2/3</code>, <code class="type">3/4</code></td>
							</tr>
					</tbody>
	</table>
</div>


## Limit the selection

### Multiple or single mode

If you only want to select a single page, set multiple mode to `false` (default is `true`)

```yaml
fields:
  related:
    label: Related Pages
    type: pages
    multiple: false
```
### Maximum number of pages

You can set the maximum number of pages that can be selected:

```yaml
fields:
  related:
    label: Related Pages
    type: pages
    max: 3
```
## Query pages

By default, the `pages` field lets you select pages from the complete page index. You can limit these options using the `query` property:

### Only children of a given page

```yaml
fields:
  related:
    label: Related Pages
    type: pages
    query: site.find('notes')
```

### Only pages with a given template

```yaml
fields:
  related:
    label: Related Pages
    type: pages
    query: page.index.filterBy('template', 'in', ['template-a', 'template-b'])
```

By default, users can also select subpages of the given selection. Use the `subpages` property to prevent this.

### Include subpages

With the subpages property you can control whether or not you want to navigate the subpages of the given parent. The default is set to `true`:

```yaml
pages:
  label: Select a related page.
  max: 3
  subpages: false
```
This will deactivate the subpage navigation and only show the first level pages (or the children of the page set in the `query` option).

## Preview images

The default (preview) image is the first image in the folder. You can configure the (preview) image for each item using the `image` option:

```yaml
image: page.image.findBy("name", "cover")
```

### Preview image from files field of subpage

You can use the image of each subpage's `files` field as preview image:

```yaml
image: page.myFilesField.toFile
```

For more examples of how to use the query language, see the <a href="https://getkirby.com/docs/guide/blueprints/query-language">guide</a>.

### Preview image from assets folder

You can also provide an image from the assets folder via a page model, for example as a fallback if the page has no images:

```php "/site/models/album.php"
<?php

class AlbumPage extends Page
{
    public function previewImage()
    {
        if ($image = $this->images()->first()) {
          return $image;
        }
        if (file_exists(kirby()->root('assets') . "/images/default.jpg")) {
            return new Asset("assets/images/default.jpg");
        }
        return false;

    }
}
```

And then in your blueprint

```yaml
image: page.previewImage
```

<info>
Note that a model only works for pages that share the same blueprint. For pages with different blueprints, use a <a href="https://getkirby.com/docs/reference/plugins/extensions/page-methods">custom page method</a> instead.
</info>

For more fine-grained control you can set further options:

### `query`

An image query, default `page.image`

```yaml
image:
  query: page.children.first.image
```

### `cover`

Whether or not the image will cover the available space.
Options: `true`, `false (default)`

```yaml
image:
  cover: true
```

### `ratio`

A freely selectable image ratio

```yaml
image:
  ratio: 16/9
```

### `back`

Set an image background.
Options: `pattern (default)`, `black`, `white`
```yaml

image:
  query: page.image.findBy("name", "cover")
  cover: true
  ratio: 1/1
  back: black
```

### No image

If you don't want to show an image but the icon selected for the page, you can set the `query` option to `false`:

```yaml
image:
  query: false
```

Kirby will then either show a default page icon or the icon defined in the page blueprint.

## Info and text

The `info` and `text` properties allow you to define what information is shown for each selected item (and in the modal). You can use Kirby's <a href="https://getkirby.com/docs/guide/blueprints/query-language">query language</a> to query any information you need.

### Text
The `text` property shows the main information for the page, by default that is the page title. You can however either modify what is shown and how:

```yaml
text: "{{ page.title.upper }}"
```

```yaml
text: "{{ page.title }} | {{ page.description.excerpt(20) }}"
```

### Info

With the `info` property, you can show additional information:

```yaml
info: "{{ page.images.count }}"
```

```yaml
info: "{{ page.tags }}"
```

<h2 id="pagination"><a href="#pagination" tabindex="-1">Pagination</a></h2>
<p>Options in the pages picker are paginated. You can set the number of items per pagination page in the picker using the <code class="type">limit</code> property. The default setting is <code class="type">20</code>.</p>
<figure class="code">
<pre><code class="language-yaml">fields:
  pages:
    type: pages
    label: Select an item
    limit: 10</code></pre>
</figure>
<h2 id="search"><a href="#search" tabindex="-1">Search</a></h2>
<p>The pages picker shows a search field by default. If you want to remove it, you can switch it off with the <code class="type">search</code> option:</p>
<figure class="code">
<pre><code class="language-yaml">fields:
  pages:
    type: pages
    label: Select an item
    search: false</code></pre>
</figure>

## How to use in templates/snippets

### Single page

To convert a single page to a page object, use the [`toPage()`](/docs/reference/templates/field-methods/to-page) method:

```php
<?php if ($relatedPage = $page->related()->toPage()): ?>
  <?= $relatedPage->title() ?>
<?php endif ?>
```

### Multiple pages

To convert multiple pages to a pages collection, use the [`toPages()`](/docs/reference/templates/field-methods/to-pages) method:

```php
<?php
$relatedPages =  $page->related()->toPages();
foreach($relatedPages as $relatedPage): ?>
  <?= $relatedPage->title() ?>
<?php endforeach ?>
```

## More information

<a href="https://getkirby.com/docs/cookbook/collections/related-articles">Related articles</a>