I have one default page template that I wish to use for two scenarios. I’d prefer to use only one page template for the sake of simplicity for my client.
Here’s what I’m trying to accomplish:
if parent_page OR if child-page without children {
display full-width-layout
}
if child page with children or if grandchild page {
display sidebar-menu-layout
}
Is this possible?
This is what I’ve tried so far:
if( is_page() && $post->post_parent > 0 ) {
//display sidebar-menu-layout
} else {
//display full-width-layout
}
It works so far as on top level pages, it displays full-width-layouts. But, what can I do to make sure that the sidebar-menu-layout is displayed on child-pages with children and on grandchile pages only? And for child-pages with no children, to display the full-width-layout.
Thanks in advance. I’m sure it has a simple solution, I’m just relatively new to WP so still trying to figure out what can and cannot be done.
2 s
Before reading the solution Bravokeyl provided I had finally, through trial and error, come up with a solution that worked for me. I’m not sure which is the better of the two, or the most correct, I only know that mine worked for me, for the problem I had.
This is the code I used to display full-width layout or sidebar-menu layout:
if( is_page() && $post->post_parent > 0 ) {
// post has parents
$children = get_pages('child_of=".$post->ID);
if( count( $children ) != 0 ) {
// display sidebar-menu layout
}
$parent = get_post_ancestors($post->ID);
if( count( $children ) <= 0 && empty($parent[1]) ) {
// display full-width layout
} elseif ( count( $children ) <= 0 && !empty($parent[1]) ) {
// display sidebar-menu layout
}
} else {
// post has no parents
// display full-width layout
}