General
Getting & Giving Support
Administration
Design
- Tut. - Layouts and Themes
- Tut. - Navigation cookbook
- Tut. - How To section
Reference Material
Plugin Development
- Tut. - Writing a plugin
- Ref. - List of Events
- Ref. - Enabling cron jobs
- Ref. - PHPDoc
Core Development
- Ref. - PHPDoc
Press information
Wolf CMS books section
children()
children() returns an array of values relating to the child pages of the current page.1) Normally, then, it is not used on its own, but to give some information about published subpages to a given page.
The array produced by children() requires a foreach loop to present usable information. The most simple listing of subpage titles, then, could look like this:
<h3>List of pages</h3>
<ul>
<?php foreach ($this->children() as $child) : ?>
<li><?php echo $child->title(); ?></li>
<?php endforeach; ?>
</ul>
In situations when in return only a single result is desired, the foreach loop should be ditched in favor of limit ⇒ 1 argument (see Arguments below in this page), otherwise it will not work and a fatal error is returned instead.
The example below returns the last published page from Articles as the parent page.
<?php $page_article = $this->find('/articles/'); $last_article = $page_article->children(array('limit'=>1, 'order'=>'page.created_on DESC')); ?> <h2 class="post_title"><?php echo $last_article->link(); ?></h2> <?php echo $last_article->content(); ?> <?php if ($last_article->hasContent('extended')) echo $last_article->link('Continue Reading…'); ?>
For further information on how to use children() in constructing menus, see how to Display a list of subpages.
Conditions
Including hidden pages
By default, children() only returns “published” pages.2) In the following line of code, the final 'true' tells Wolf to include hidden pages as well:
$this->children(null,array(),true)
Additional arguments
Four more arguments can be given to children() to further define the subpages it returns:
- where - sets a condition
- order - determines the sort order (by field name in page table [see note below], either ASC ascending, or DESC descending)
- offset - where in the list of subpages to begin the list
- limit - how many pages to return
A note on “order”
Any of the fields in the page table can be used to sort your “children” pages. In first example, below, you could have:
'order' => 'title ASC'
to arrange the order by the page Title in A-Z order, or
'order' => 'slug DESC'
to order the list by “slug” value in Z-A order. That should give you the idea! While any value in the “page” table could be used here, the main options would include:
- title
- slug
- breadcrumb
- created_on
- published_on
- updated_on
- created_by_id
- updated_by_id
The default is position, which is set automatically when the drag-drop page re-ordering is used.
Examples
Example 1: Last five pages
<?php $last_result = $this->children(array('limit' => 5, 'order' => 'created_on DESC')); ?>
Then you need to use $last_result instead of $this->children() in the foreach loop described above to produce the list. Adjusting the simple code above for listing the titles of the last five pages would look like this:
<h3>Last five pages</h3>
<ul>
<?php $last_result = $this->children(array('limit' => 5, 'order' => 'created_on DESC')); ?>
<?php foreach ($last_result as $child) : ?>
<li><?php echo $child->title(); ?></li>
<?php endforeach; ?>
</ul>
Example 2: Only certain pages
If you only wanted to list results 3 and 4, for example, ordered by the position (by default), you would use:
<?php $last_result = $this->children(array('offset' => 2, 'limit' => 2)); ?>
Since PHP starts counting at “0”, the offset needs to starts at 0 not at 1. Thus setting the offset at “2” actually gives you the third page. Again, as in the previous example, $last_result is used in the foreach loop to produce the desired list.
Example 3: Order hidden articles by published date
To return a list of 10 hidden articles by published date for example, use the following:
<?php $last_result = $this->find('/articles/')->children(array('order'=>'published_on DESC','limit'=>10),array(),true); ?>
