0

I'm trying to list out all the child pages of a custom page I've made.

In my custom page, I'm doing this to list out the pages:

<?php get_header(); ?>

<?php

$subpages = new WP_Query( array(
    'post_type' => 'page',
    'post_parent' => $post->ID,
    'posts_per_page' => -1,
    'orderby' => 'menu_order'
));

if ($subpages->have_posts()) : while ($subpages->have_posts()) : the_post(); ?>

    ...

<?php endwhile; else : ?>

   ...

<?php endif; ?>

<?php get_footer(); ?>

But this isn't working. Now it only lists out itself (the parent, which I don't want listed out at all), and none of its children.

Also, the sites just keeps loading, while everything except the footer is displayed. It's like its returning one half of the page, and then hangs as it works on the rest. While running this my CPU and fan suddenly kicks in.

Does anyone know why?

1

I tested the query and it does work so the only thing I can spot that is misleading but not right is directly after you while() statement you have the_post(). This doesn't work in secondary queries, it should look like:

<?php if( $subpages->have_posts() ) : ?>

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

        ...

    <?php endwhile; ?>

<?php endif; ?>

Note the $subpages->the_post(); - since it's a secondary query we need to continue referencing the query variable. After this all the normal loop functions work as expected, such as: the_title(), the_content(), etc.

  • That worked great, thanks! Also explains why it listed it self out :) – ptf Jul 9 '15 at 19:51
-1
<?php 
global $post;
$child_pages_query_args = array(
    'post_type'   => 'page',
    'post_parent' => '93',
    'orderby'     => 'date DESC'
);
$child_pages = new WP_Query( $child_pages_query_args );
?>

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

<div class="col-lg-6">
<div class="featured-pwrap">  
    <?php if ( has_post_thumbnail() ) { the_post_thumbnail('home-pages-thumb'); } else { ?>
    <img class="attachment-home-pages-thumb" src="<?php bloginfo('template_directory'); ?>/images/default-image.jpg" alt="<?php the_title(); ?>" />
    <?php } ?>
<h3><?php the_title(); ?></h3>
<a href="<?php the_permalink(); ?>" class="futured-more">Read more</a>
</div>
</div>
<?php endwhile; endif; wp_reset_postdata(); ?>
  • Hi Andrey, welcome to WPSE. Please feel free to take the tour. Would you mind editing your post to both format the code correctly, and also explain what your code is doing? Thanks very much! – Tim Malone May 3 '16 at 4:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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