1

I was wondering as to how I can list the child of a child pages in wordpress.

Here is what I have set up.

Videos (Parent)
 -Music Videos(child)
  --Video 1 (Child of Child)
  --Video 2 (Child of Child)

 -Performances (Child)
  --Video 1 (Child Of Child)
  --Video 2 (Child Of Child)

I want to list all the "child of child" of the parent page "VIDEOS".

I know this code only list the "child" pages but not the child of childs.

<?php
$child_pages = $wpdb->get_results("SELECT *    FROM $wpdb->posts WHERE post_parent = ".$post->ID."    AND post_type = 'page' ORDER BY menu_order", 'OBJECT');    ?>
<?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
<div class="child-thumb">
<?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>
<a href="<?php echo  get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a>
</div>
<?php endforeach; endif;
?>

I want to do this without having to add anything to my functions.php

What do I need to add to make this happen. I have five child of child pages under the Parent page.

1

1 Answer 1

1

First of all, why you are querying for posts by your custom query? We have WP_Query class and get_posts function to do this.

$child_pages = $wpdb->get_results("SELECT *    FROM $wpdb->posts WHERE post_parent = ".$post->ID."    AND post_type = 'page' ORDER BY menu_order", 'OBJECT');

became

$pages = get_posts(array(
    'post_type' => 'page',
    'orderby' => 'menu_order'
    'order' => 'ASC'
    'posts_per_page' => -1,
));

Next, you should use get_page_children function (get_page_children on codex) to generate tree list. get_page_children does not query to database each time we want to generate list of children, but is using list of all posts which is stored in $pages variable.

<ul>
    <?php foreach ($pages as $page): ?>
        <?php if ($page->post_parent == 0): ?>
            <li>
                <?php echo $page->post_title; ?>
                <?php $children = get_page_children($page->ID, $pages); ?>
                <ul>
                    <?php foreach ($children as $child): ?>
                        <?php if ($child->post_parent == $page->ID): ?>
                            <li>
                                <?php echo $child->post_title; ?>
                                <?php $granchildren = get_page_children($child->ID, $pages); ?>
                                <ul>
                                    <?php foreach ($granchildren as $grandchild): ?>
                                        <li><?php echo $grandchild->post_title; ?></li>
                                    <?php endforeach; ?>
                                </ul>
                            </li>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </ul>
            </li>
        <?php endif; ?>
    <?php endforeach ?>
</ul>

EDIT. I think that this will fit to your expectation now:

<?php $posts = get_posts(array('posts_per_page' => -1, 'post_type' => 'page')); ?>
<?php $children = get_page_children($post->ID, $posts); ?>
<?php foreach ($children as $child): ?>
    <?php if ($child->post_parent == $post->ID): ?>
        <div>
            <h2><?php echo $child->post_title; ?></h2>
            <ul>
                <?php $grandchildren = get_page_children($child->ID, $posts); ?>
                <?php foreach ($grandchildren as $grandchild): ?>
                    <li><?php echo $grandchild->post_title; ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>
<?php endforeach; ?>

enter image description here

11
  • I'm trying to list them all together, Like we do the post on the homepage. Like whenever I add a new grandchild page it automatically updates on the videos page. Aug 17, 2016 at 7:18
  • So you should use only once get_page_children, it will give you all children of given post parent ID Aug 17, 2016 at 7:19
  • Do I copy the code just as is and use it? Aug 17, 2016 at 7:25
  • I'm having a hard time understanding what your code above is doing. Aug 17, 2016 at 7:34
  • Look at answer now, I think that this is what you want to achieve Aug 17, 2016 at 7:38

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