10

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 Answers 2

7

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
}
4
Level-0
--Level-1
----Level-2
------Level-3
----Levelanother-2
--Levelanother-1

check whether page is top level page (it may have children or not) ?

$post->$post_parent == 0 or is empty get_post_ancestors( $post ) returns only Level-0 Pages.

Is a child page and is a Level-1 page or Levelanother-1 only ?

$post->$post_parent > 0 or is not empty get_post_ancestors( $post ) and is empty get_post_ancestors( $post->post_parent )

Is level-1 page but doesn't have children like Levelanother-1 page ?

$post->$post_parent > 0 or is not empty get_post_ancestors( $post ) and is empty get_post_ancestors( $post->post_parent ) and count(get_children( $post ->ID, 'ARRAY_A' )) == 0 ..

I didn't check this yet..but it should work fine. You can also play with get_page_children() and get_posts()

2
  • Thank you bravokeyl. That solution worked for me. After a lot of trial and error (mainly error) I came up with a solution of my own earlier today before I read your reply. I'll post that below.
    – laura.f
    Mar 25, 2015 at 20:58
  • Great! you have found solution.
    – bravokeyl
    Mar 25, 2015 at 22:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.