Display a list of subpages
The ‘status’ of your page will determine how it can be listed. This guide assumes you are looking for “Published” pages. Here is the example of the header snippet:
<ul> <li><a href="<?php echo BASE_URL; ?>">home</a></li> <?php foreach($this->find('/')->children() as $menu): ?> <li><?php echo $menu->link(); ?></li> <?php endforeach; ?> </ul>
LI is added to link the Home Page.
BASE_URL is a constant that Frog uses to display the
link() or whole URL in the application. You can use it without any problem.
Then you have the
$this->find('/') method that will return the page at the URL that you send it to, in this case, the Home Page.
The you have the
children() method that will return all subpages of the page already found. (You can do
$this->children() and you will get all the “published” and “reviewed” subpages of the current page.)
Then you have a “foreach” loop that will pass each subpage one by one and execute every line between this line and the
<?php endforeach; ?>, in this case the line that will be repeated is:
<li><?php echo $menu->link(); ?></li>. (There is another page with more information about making links.)
If you take a look at the HTML source code of the page header you will see that the PHP code above has produced something like this:
<ul> <li><a href="http://localhost/frog/?">Home</a></li> <li><a href="http://localhost/frog/?about_us" title="About us">About us</a></li> <li><a href="http://localhost/frog/?articles" title="Articles">Articles</a></li> </ul>
What else can I do with
- You can decide to add a condition to the search (be careful with this one!)
- You can limit the number of results returned (limit)
- You can change the offset to be returned (offset)
- You can determine the order (by field name in
pagetable [see note below], either ASC ascending, or DESC descending)
- That’s enough power I think!
if you want to display the last 5 results only, here is how to do it:
<?php $last_result = $this->children(array('limit' => 5, 'order' => 'created_on DESC')); ?>
Then you need to loop those results like the example in the previous HOW-TO.
if you only want results 3 and 4, ordered by the position (by default):
<?php $last_result = $this->children(array('offset' => 2, 'limit' => 2)); ?>
This might appear weird to you but … the offset starts at 0 not at 1.
A note on “order”
Any of the fields in the
page table can be used to sort your “children” pages. In Example 1, above, 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:
The default is
position, which is set automatically when the drag-drop page re-ordering is used.