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
}
Related Posts:
- Is there a default template file for child pages / subpages?
- Only allow new subpages to be created
- Custom page template for multiple pages
- Keep page hierarchy in wp_list_pages, even if on a child or grandchild
- Search anything from a Child and Sub-Pages of their Parent page
- Custom templates not showing up in template dropdown
- Conditional tag to check if ‘page.php’ is being used?
- WordPress page edit does not save selected template
- Allow only new sub-pages to be created
- Set page template automatically based on parent
- Can I display submenus in groups, using wp_list_pages?
- Get Permalink for the top level parent of child pages
- Some pages are missing from the Parent Page select in the Editor
- How to display code if it is not certain pages?
- Multiple areas of dynamic content in a page
- How do I set and list page_template with wpcli?
- How to display list of child pages of parent page in wordpress?
- How to inject custom url path for page ?
- Add custom admin menu item for pages using a certain template
- Different “Text Widgets” in Sidebar on Many Different Pages?
- What’s the most minimal way in which a page can be hooked into WP?
- How do I create multiple page while active a theme
- How to make certain page templates visible to admin only
- How to dynamically save a selected option from page “Templates” selectbox?
- Show child pages when on a child page
- select a private page as parent for another private page
- Dynamically change page that loads
- Set order of returned items in the WP_Query() class/function
- How can I automatically add child pages to pages in a WP menu?
- load common thank you template with different content for different pages
- Password Protected Page Title
- remove/hide pages from users backend
- Removing the TinyMCE editor for a given page template
- List child pages by slug not ID?
- How to hide all child pages with post_query?
- get_pages() & “child_of”
- Trying to return list of child pages for a template
- Removing Parent Page URL While Keeping the Navigation Intact
- Organizing Your Pages
- Display child pages in sidebar of parent page in wordpress
- Display child pages in a parent page? [closed]
- Redirect page to default subpage
- How to edit a HTML list in WordPress?
- Custom page with queries returns 404
- How to capture GET argument on static page
- List all posts with the parent or grandparent of a page with a specified ID
- JQuery Plugins in WordPress
- the_content of template page
- WordPress theme: Add page content to the bottom of the Contact form
- Dynamic href link to Contact Page
- Create page template with some static contents
- WordPress 3.2 – removed ability to select a page template in the page attributes when adding a new page?
- Page access only from a specific page in wordpress website
- Use same page-mypage.php template for several pages
- Avoid pages losing template association on site re-deploy
- How do you implement a page template automatically being applied to a page?
- How do I edit text displayed on my browser tab?
- How to create different template designs for different pages in WordPress [closed]
- require_once not working
- Displaying posts on homepage – Template Page
- Page templates in subdirectories and auto generator
- WordPress Page doesn’t load the template selected
- Displaying direct children of other pages on homepage
- Use page template for custom $_GET content
- Get all children titles of a page as a link to them
- How to bypass 404 for certain pages/posts?
- Outputting content from page
- Custom folder for wordpress page templates
- How to control display of page lists on sidebar that doesn’t have childrens
- Multiple Loops That Pull Pages (Child/Grandchild) While Styling the First Entry
- Exclude child pages args array
- custom page templating in WordPress
- page template – undefined structure in source code
- Show children connected to parent pages
- How to get an array of pages ID by some page’s slug and all its children pages in get_posts() function?
- WordPress blog posts template bug [closed]
- If page or sub page not working as expected
- Adding styles/scripts to specific page
- WordPress form submit to custom page template does not work
- How to use wp_dropdown_pages or wp_list_pages to accomplish a menu like this?
- Using external DB within wordpress
- Displaying Child Page’s Information
- Load one page with parent and child pages
- WordPress Page All Content In Template Good Or Bad Convention?
- Display inner pages like posts.[Like how post excerpt works]
- Childpage title + content on Parent Page
- Content template when adding a new page
- Pages redirecting to homepage
- Avada Theme – Display results from a specific category [closed]
- How to display time when page is created and edited
- Call a function on every wordpress pages only
- Firing page_publish where page is child page
- WP Template Hierarchy – Need help choosing the correct templates
- how bbPress and buddypress add their own page templates to the page template drop down on the page editing screen
- displaying child pages in columns when on a parent page
- Showing certain page without redirection
- How to make in post Parent page children page list nummbered
- Create a new page for each form selection
- List of pages – AJAX load more
- May I know where to edit the tax rate?