0

I thought this would be a simple case of following the Codex but apparently not. I'm trying to display child pages of a particular page on my homepage using a custom made template. So far I've managed to display what I want in terms of the content but I can't achieve my goal which is to display child pages of one selected page.

The page I want has an ID of 37. Yet using child_of or parent it ignores it and shows every single page. Below if my code:

                    <?php $home_pages = new WP_Query(array(
                        'sort_order'    => 'DESC',
                        'sort_column'   => 'post_date',
                        'post_type'     => 'page',
                        'post_status'   => 'publish'
                    ));
                        if( $home_pages->have_posts() ) : ?>
                        <ul>
                            <?php $pages_per = 0; 
                            while( $home_pages->have_posts() && $pages_per < 6 ) : $home_pages->the_post(); ?>
                                <li>
                                    <?php if ( has_post_thumbnail() ) : ?>
                                        <?php the_post_thumbnail(); ?>
                                    <?php else : ?>
                                        <img src="images/noimage.png" border="0" alt="No Thumbnail" />
                                    <?php endif; ?>
                                    <a href="<?php the_permalink(); ?>" class="title"><?php the_title(); ?></a>
                                    <?php $pages_per++; ?>
                                </li>
                            <?php endwhile; ?>
                        </ul>
                        <?php endif; ?>
                        <?php wp_reset_postdata(); wp_reset_query(); ?>

The code shows six pages as instructed in the loop but doesn't seem to filter out any pages. The above example doesn't use child_of and parent but that's because I tried them and it didn't work.

For reference: https://codex.wordpress.org/Function_Reference/get_pages

  • You're mixing get_pages() parameters into your WP_Query call - so what is it what you actually want to use here? – Nicolai Apr 13 '15 at 22:29
0

I figured it out. It looks as if the Codex never referenced the one parameter I needed to make it work in the Codex. I checked out this site here for a full list of parameters in the arrays http://www.billerickson.net/code/wp_query-arguments/

I needed to use post_parent and not child_of or parent. Fixed.

  • Yea, you needed to read the correct documentation, the one for WP_Query, thats all :D But, hey, you solved it! – Nicolai Apr 13 '15 at 22:32
  • That's true! If I hadn't of asked this question I know I'd still be stuck on it. Oh well. Hopefully someone else finds this useful too! :) – user69700 Apr 13 '15 at 22:33
  • Someone usually does. – Nicolai Apr 13 '15 at 22:34

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.