Last active
June 16, 2016 03:49
-
-
Save antcms/011b7ecb3aa28555f068384f2d6a6d06 to your computer and use it in GitHub Desktop.
Change the WordPress Template for page.php if it has children
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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