The $page API variable

The $page variable is provided to every template, and it contains all the content (via fields) specific to the page being viewed.

This includes both built-in fields, which are common to all pages, as well as the custom fields. The custom fields are those that you define in Admin > Setup > Fields and then assign to your template in Admin > Setup > Templates. $page also has several functions/methods that enable you to perform other tasks with it. The built-in fields and functions/methods are documented on this page.

Custom Fields

Custom fields are those that you have defined with the page's template. You can always access these by referencing them from the $page variable directly. For example, lets say that you have defined a field called "subtitle" and you want to output it in a headline:

echo "<h2>" . $page->subtitle . "</h2>";

$page API reference

See details about the $page variable in our API reference.

Usage Examples

Make use of the $page->parent variable:

echo "The title of the parent page is {$page->parent->title}";
if(!$page->parent->id) echo "You are on the homepage";

Generate a breadcrumb trail:

foreach($page->parents as $parent) {
  echo "<li><a href='{$parent->url}'>{$parent->title}</a></li>";
}

Display the name of the template:

echo "This page is using the template: {$page->template->name}";

Display the name of each field on the page, along with it's value:

foreach($page->fields as $field) {
  echo "<p>{$field->name}: " . $page->get($field->name) . "</p>";
}

Display the number of children, followed by navigation to all of them:

echo "<p>This page has " . count($page->children) . " children</p>";
foreach($page->children as $child) {
  echo "<li><a href='{$child->url}'>{$child->title}</a></li>";
} 

Find 3 of the most recent children that have a featured checkbox set, and display links to them:

$features = $page->children("featured=1, limit=3, sort=-date");
foreach($features as $feature) {
  echo "<p>Featured story: <a href='{$feature->url}'>{$feature->title}</a></p>";
}

Display a list of paths to all sibling pages, not including the current page:

foreach($page->siblings as $sibling) {
  if($sibling->id != $page->id) echo "<li>Sibling: {$sibling->path}</li>";
}

Display a link back to the first page, if the current pageNum is greater than 1:

if($page->pageNum > 1) {
  echo "<a href='{$page->url}'>Back to first page</a>";
}

Iterating a $page

Iterating a $page produces the name and value of all the page's custom fields. Here is an example to illustrate this:

foreach($page as $field => $value) {
  echo "$field = $value <br />";
}

This is simpler than iterating through $page->fields and retrieving each value manually, as we did in one of the previous examples. The result might look like this:

title = About ACME Inc.
subtitle = The Leader in Something
body = Since 2010, ACME Inc. has been the leader in something, and so on...

Twitter updates

  • New post: This week we take a look at what’s in ProcessWire 3.0.126 which focuses largely on resolving issue reports, but also includes a handy new Page if() method— More
    15 February 2019
  • New post: ProcessWire 3.0.125 has several useful new Sanitizer methods & options, plus new ways to access them directly from the Input API variable. This makes handling user input even easier than before. Plus updates to our i18n functions & API docs— More
    25 January 2019
  • New post: In this week’s post, we’ll take a look a look at the new website and focus on some parts of it and how they were built. Then we’ll dive into the latest version of ProcessWire on the dev branch, version 3.0.124— More
    11 January 2019

Latest news

  • ProcessWire Weekly #249
    In the 249th issue of ProcessWire Weekly we're going to walk you through the latest core updates (ProcessWire 3.0.126), introduce a new third party module called Page Access Releasetime, and pick a new site of the week. Read on!
    Weekly.pw / 16 February 2019
  • ProcessWire 3.0.126 core updates
    This week we take a look at what’s in ProcessWire 3.0.126 which focuses largely on resolving issue reports, but also includes a handy new $page->if() method. 
    Blog / 15 February 2019
  • Subscribe to weekly ProcessWire news

“To Drupal, or to ProcessWire? The million dollar choice. We decided to make an early switch to PW. And in retrospect, ProcessWire was probably the best decision we made. Thanks are due to ProcessWire and the amazing system and set of modules that are in place.” —Unni Krishnan, Founder of PigtailPundits