$page always represents the current page being viewed

The $page variable is provided to every template, and it contains all the 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>";

Reference for .$page and all Page instances

Built-in Fields Reference

$page->idThe numbered ID of the current page
$page->nameThe name assigned to the page, as it appears in the URL
$page->titleThe page's title (headline) text
$page->pathThe page's URL path from the homepage (i.e. /about/staff/ryan/)
$page->urlThe page's URL path from the server's document root (may be the same as the $page->path)
$page->httpUrlSame as $page->url, except includes protocol (http or https) and hostname.
$page->parentThe parent Page object or a NullPage if there is no parent.
$page->parent_idThe numbered ID of the parent page or 0 if none.
$page->parentsAll the parent pages down to the root (homepage). Returns a PageArray.
$page->rootParentThe parent page closest to the homepage (typically used for identifying a section)
$page->templateThe Template object this page is using
$page->fieldsAll the Fields assigned to this page (via it's template, same as $page->template->fields). Returns a FieldsArray.
$page->numChildrenThe number of children (subpages) this page has.
$page->childrenAll the children (subpages) of this page.* Returns a PageArray. See also $page->children($selector). 
$page->childThe first child of this page. Returns a Page. See also $page->child($selector).
$page->siblingsAll the sibling pages of this page.† Returns a PageArray. See also $page->siblings($selector). 
$page->nextThis page's next sibling page, or NullPage if it is the last sibling.† See also $page->next($pageArray). 
$page->prevThis page's previous sibling page, or NullPage if it is the first sibling.† See also $page->prev($pageArray). 
$page->pageNumThe current page number (1 or greater), only used if the page's template allows pagination. Deprecated, see $input->pageNum instead.
$page->createdUnix timestamp of when the page was created
$page->modifiedUnix timestamp of when the page was last modified
$page->createdUserThe user that created this page. Returns a User or a NullUser.
$page->modifiedUserThe user that last modified this page. Returns a User or a NullUser.

Built-in Functions/Methods Reference

$page->find($selector)Find pages matching the selector anywhere below this page (children, grandchildren, etc.). Returns a PageArray.
$page->children($selector)All the children (subpages) of this page,  filtered by a selector.* Returns a PageArray.
$page->child($selector)The first matching child (subpage) that matches the given selector. Returns a Page, or a NullPage if no match.
$page->siblings($selector)All the sibling pages of this page, filtered by a selector.† Returns a PageArray.
$page->next($pageArray)Given a PageArray which includes the current page (among others), return the next page after the current.† Returns a NullPage if it is the last page in the provided PageArray. If called without a PageArray, it assumes the current page's siblings (same as $page->next).
$page->prev($pageArray)Given a PageArray which includes the current page (among others), return the previous page before the current.† Returns a NullPage if it is the first page in the provided PageArray. If called without a PageArray, it assumes the current page's siblings (same as $page->prev).
$page->save()Save this Page
$page->save($field)Save just the field given by the provided field name (string) or object (Field)
$page->delete()Delete this page
$page->is($name)Returns true if this page matches the given template name, page status, or selector.   
$page->isChanged()Has the $page changed since it was loaded? Returns true or false.
$page->isChanged("field")Has the given field on $page changed since it was loaded? Returns true or false.
$page->matches("selector")Returns true if this page matches the given selector string. 
$page->set("field", $value)Set the given custom field name to have the provided value
Same as $page->$field = $value;
$page->get("field")Get the value for the specified field or null if the field isn't part of this page  
Same as $page->$field
$page->get("field1|f2|f3")Get the first matching non-empty field by specified by a string of pipe "|" separated field names
$page->getUnformatted("field")Get the unformatted value of the given field name. May also be used with "|" separated field names (mentioned above). 
$page->setOutputFormatting(true|false)By default, output will be formatted according filters you may have defined with the field.  If you are modifying the values of a page's custom fields, you will need to call $page->setOutputFormatting(false) before doing so. This turns off output formatting, which ensures that saved values don't already have runtime formatters applied to them. ProcessWire will throw an error if you attempt to save formatted fields.
$page->of(true|false)Shorter version of setOutputFormatting(). Sets the outputFormatting state. 
$page->of()Returns the current outputFormatting state (returns true or false). 
$page->editable()Is the page is editable by the current user? Returns true or false.
$page->editable('field')Is the given field name is editable on the page by the current user? Returns true or false.
$page->viewable()Is the page viewable by the current user? Returns true or false. Note that this is only useful on pages other than the current page being viewed, as ProcessWire won't let the user load a page (by URL) that they don't have access to view.
$page->addable()Is the current user allowed to create pages below this page? Returns true or false.
$page->addable($addPage)Is the current user allowed to add the given page ($addPage) as a child of $page? Returns true or false.
$page->moveable()Is the page allowed to be moved by the current user? Returns true or false.
$page->moveable($parent)Is the current user allowed to move $page to $parent? Returns true or false.
$page->sortable()Is the page sortable (within the same parent) by the current user? Returns true or false. 
$page->deleteable()Is the page deleteable by the current user? Returns true or false.
$page->publishable()Is the page publishable by the current user? Returns true or false. 

*The children property may not be efficient to use when the page has a lot of children. Care should be taken on large sites in consideration of the fact that this property will load all child pages. In large scale use, you should make use of the function/method equivalent version: children($selector) and enforce a "limit=n" portion in your $selector so that you can control the number of pages loaded. For more details, see pagination.

†The siblings, next and prev properties and methods may not be efficient to use when the page has a lot of siblings. Care should be taken on large sites in consideration of the fact that these properties will load all sibling pages. In large scale use, you should make use of the function/method equivalent versions: siblings($selector), next($pageArray), and prev($pageArray), so that you can control the number of pages loaded.

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...

See Also

$pages
PageArray
NullPage

Comments

  • apeisa

    Posted by apeisa on Jan 23, 2011 7:12 AM

    There is no mention about useful method $page->editable()

    Default site uses them like this:

    // If the page is editable, then output a link that takes us straight to the page edit screen:

    if($page->editable()) {

    echo "Edit {$session->get($name)} jee";

    }

  • apeisa

    Posted by apeisa on Jan 23, 2011 2:04 PM

    Correction to the above code snippet (there was some of my test code):

    // If the page is editable, then output a link that takes us straight to the page edit screen:

    if($page->editable()) {

    echo "Edit";

    }

  • Alan Bristow

    Posted by Alan Bristow on Mar 7, 2012 4:48 PM

    Correction to "Generate a breadcrumb trail", the code sample ends

    ';

    but should end

    ";

  • Carlos

    Posted by Carlos on Mar 9, 2012 3:10 PM

    The last Usage Example before "Iterating a $page", uses

    $page->pageNum which is deprecated for $input->pageNum.

  • onjegolders

    Posted by onjegolders on Apr 6, 2012 10:48 AM

    Phenomenal documentation!

    This is the level of detail that others should aspire to...

  • hachim

    Posted by hachim on Mar 26, 2014 9:19 AM

    Hello,

    I'm new here. I started using processwire recently.

    How to contribute to this documentation?

    For example adding examples, keep it up to date....

    Thanks

  • ryan

    Posted by ryan on May 26, 2014 8:44 AM

    Hachim: Please post in the comments form on the relevant page (like this) if you see something that should be added or changed. Or post in our forum at processwire.com/talk/

Post a Comment

Your e-mail is kept confidential and not included with your comment. Website is optional.