If/Else child list for Custom Post Type single template within loop?

I’m trying to add a feature to my ‘Services’ custom post type single template that displays one thing if the post type has one or more child post types and another thing if it doesn’t. So far I’ve come up with this, however it doesn’t seem to work as expected:

$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); else
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0"); ?>
<?php if ($children): ?>

<div class="two-column align-left spacer">
<?php echo $children; ?>

<article class="four-column spacer align-left <?php post_class(); ?>"id="post-<?php the_ID(); ?>" role="article">

<?php the_content(); ?>


<?php else : ?>

<article class="six-column spacer align-left" id="post-<?php the_ID(); ?>" role="article">

<?php the_content(); ?>


<?php endif; ?>

Can anyone help me and point me in the right direction please? I have set up the Services post type to hierarchical => true and capability_type => post (and have tried page) but still no luck.

Obviously the code above needs to sit within the standard page loop. Does this mean I need to use query_posts instead?

Many thanks,


Solutions Collecting From Web of "If/Else child list for Custom Post Type single template within loop?"

If you’re using custom post types then you’re going to need to create a custom loop.

<?php $loop = new WP_Query( array( 'post_type' => 'services', 'posts_per_page' => 5) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php endwhile; ?>

You must also have the “page-attributes”-value present in your “supports”-parameter in addition to hierarchical set to true. This is a parameter in the register_post_type-function you are using when declaring your custom post type.

Check if the supports-parameter contain “page-attributes” in your functions.php or plugin where you initiate your custom post type 🙂

Reference: https://codex.wordpress.org/Function_Reference/register_post_type#Parameters