0
votes

I have a custom post type called "films" and most films have a subpage(child) called "press".

I am trying to loop through the films, and check to see if a subpage exists. IF a subpage exists, loop through the content, and if it doesn't exist, leave out the content. My loop is as follows:

<?php $loop = new WP_Query( array( 'post_type' => 'films', 'posts_per_page' => 8,'orderby' => 'date', 'order' => 'ASC', 'film-categories' => 'available-now-shows-on-homepage' ) ); ?>

<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

<?php endwhile; ?>

I only want the linked title to show if it's page has children (a press page). How would I modify the loop to do just that?

1

1 Answers

0
votes

I just figured out the answer to my own question. It works by counting the number of children pages using get_pages. Here is the working code:

<?php
$children = get_pages( array('child_of' => $post->ID,'post_type'=>'custom-post-type-name'));
if( count( $children ) != 0 ) { ?> 

<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>

<?php }
else {  }
?>