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 client who wants the following;

They have a series of pages, each of which have children. On the parent pages they want to show links to all the children. For this I am currently using this query;

<?php
    $args = array(
        'post_type' => 'page',
        'post_parent' => $post->ID,
        'posts_per_page' => -1
    );
    $system_query = new WP_Query($args);
    while ($system_query->have_posts()) : $system_query->the_post();
?>

    <!-- links to all this page's children -->

<?php endwhile; wp_reset_postdata();?>

This works fine for the parent pages, but now the client wants to show the same set of links to show on the child pages aswell...

Is there a simple way to amend this query to allow that? Or will I have to write something else from scratch

share|improve this question
    
Check this link may be your solution –  Ravs May 22 '13 at 19:10
add comment

1 Answer 1

This seemed to work nicely for me;

<?php
    $children = get_pages('child_of='.$post->ID);
    if( count( $children ) != 0 ) {
        $id = $post->ID;
    } else {
        $id = $post->post_parent;
    }
    $args = array(
        'post_type' => 'page',
        'post_parent' => $id,
        'posts_per_page' => -1
    );
    $system_query = new WP_Query($args);
    if ($system_query->have_posts()) :
    while ($system_query->have_posts()) : $system_query->the_post();
?>

    <!-- list of child pages here -->

<?php endwhile; endif; wp_rest_postdata(); ?>
share|improve this answer
    
This will only work if your child pages are only a single level deep. Have a look at the get_ancestors function for a simpler (and less memory-intensive) method. –  Milo May 22 '13 at 20:41
add comment

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.