0
votes

Is there any way to add/change allowed template of page children ? When I create child for specific page I would like to set template to use. I have that option when I use Processwire CMS:

Select the template(s) that will be allowed for children of pages using this template. Use this only if you specifically want to restrict placement of pages using this template. If none are selected then any are allowed, within the user's access limits. An example usage could be a 'news-list' template that is only allowed to have children using 'news-item' or 'press-release' templates.

1

1 Answers

1
votes

I have come across this issue before and I have used a custom function to determine what level a page is at and then include a file based on that using:

 get_post_ancestors();

The function (added to functions.php)

function hierarchy_level($post){
    if(count(get_post_ancestors($post)) === 0){
        return 'parent';
    }elseif(count(get_post_ancestors($post)) === 1){
        return 'child';
    }
    elseif(count(get_post_ancestors($post)) === 2){
        return 'grandchild';
    }else{
        return false;
    }
}

And usage in a page template:

$page_level = hierarchy_level($post);

switch ($page_level){
    case 'parent':
        include('template-parts/parent.php');
        break;
    case 'child':
        include('template-parts/child.php');
        break;
    case 'grandchild':
        include('template-parts/grandchild.php');
        break;
}