Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom post type called "films", and most films have a subpage (child page) 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. 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?

share|improve this question

1 Answer 1

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 {  }
?>
share|improve this answer
1  
Please accept your answer (using the green tick) –  bungeshea Dec 13 '12 at 3:19
    
And get a badge in the process as well! –  kidakaka Dec 13 '12 at 5:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.