How to write a template for multiple levels?

Now I create a page called projects, and it has 2 subpage called projects1 and projects2 respectively.
The template for these 3 pages is “projects”.
Then the 3rd level page’s template is “project”.
just like:
projects----projects1----project1
----project2
----projects2----project3
----project4

How do I write “projects” template?

That depends on what you want to show on the parent projects page and the two subpages projects1 and projects2 respectively.

  1. I want projects page list all project, include grandchildren page.
    ex. projects content is project1 … project4.

2.projects and projects1 projects2 use one template.

You can use an if statement that checks the page depth:

<?php
if($page->depth() === 1): // this will happen when on the projects page
  // loop through subpages of projects page
  foreach($page->children() as $child):
    echo $child->title();
    // then loop through the grandchildren
    foreach($child->children() as $grandchild):
      echo $grandchild->title();
    endforeach;
  endforeach;
else: // this will happen when on projects1 or projects2
  foreach($page->children() as $child):
    echo $child->title();
  endforeach;
endif;  
?> 

Thank you very much, let me try:smiley:

Yeah! It’s OK!

<?php 
if($page->depth() === 1):
$projects = $page->grandChildren()->visible();
else:
$projects = $page->children()->visible();
endif;
foreach($projects as $p):
?>
project lists...
<?php endforeach ?>