Skip to content

Instantly share code, notes, and snippets.

@antcms
Last active June 16, 2016 03:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antcms/011b7ecb3aa28555f068384f2d6a6d06 to your computer and use it in GitHub Desktop.
Save antcms/011b7ecb3aa28555f068384f2d6a6d06 to your computer and use it in GitHub Desktop.
Change the WordPress Template for page.php if it has children
<?php get_header(); ?>
<?php
if (have_posts()) : while (have_posts()) : the_post();
$pid = get_the_ID();
$args = array(
'post_parent' => $pid,
'post_type' => 'page', // Change as per post type
'numberposts' => -1,
'post_status' => 'publish'
);
$childp = get_children( $args );
$numposts = count ( $childp );
if( $numposts == 0 ) {
get_template_part( 'page-single' ); // Template part if no children
}
else {
get_template_part( 'page-parent' ); // Template part if has children
}
endwhile; endif;
?>
<?php get_footer(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment