Loop that displays PARENT PAGE & CHILD PAGE & outputs GRANDCHILD PAGE title and content

I’ve never posted here, but I’m tired of slapping code together. That said, I would be most appreciative if someone could help me with this.

I need a page template that:

  1. outputs the PARENT PAGEs,
  2. the CHILDREN PAGES of that PARENT PAGE,
  3. and finally output the titles and content of the GRANDCHILDREN PAGES so I can insert them into a tabbed layout.

Here’s a diagram:

enter image description here

The idea is that visitors should be able to get to the content on the GRANDCHILDREN PAGES in two clicks.

For the PARENT PAGES, I’m using wp_nav_menu.

For the CHILD PAGES, I’m using:

    <?php if ( is_page() ) { ?>
    <?php
    if($post->post_parent)
    $children = wp_list_pages('title_li=&child_of=".$post->post_parent."&echo=0'); else
    $children = wp_list_pages('title_li=&child_of=".$post->ID."&echo=0');
    if ($children) { ?>

    <ul class="child-pages">
    <?php echo $children; ?>
    </ul>
    <?php } } ?>

And to output the GRANDCHILDREN PAGES title and content, I’m using some code from the codex:

GRANDCHILDREN PAGES

<ul class="nav nav-tabs" id="myTab">
<?php
$pages = get_pages('child_of=".$post->ID."&sort_column=post_date&sort_order=desc');
$count = 0;
foreach($pages as $page)
{
    $content = $page->post_content;
    if(!$content)
        continue;
    if($count >= 100)
        break;
    $count++;
    $content = apply_filters('the_content', $content);
?>

<li>
<a data-toggle="tab" href="#<?php echo $count ?>"><?php echo $page->post_title ?></a>
</li>

<?php } ?>
</ul>

GRANDCHILDREN CONTENT

<div class="tab-content">

<?php
    $pages = get_pages('child_of=".$post->ID."&sort_column=post_date&sort_order=desc');
    $count = 0;
    foreach($pages as $page)
    {
        $content = $page->post_content;
        if(!$content)
            continue;
        if($count >= 100)
            break;
        $count++;
        $content = apply_filters('the_content', $content);
    ?>


<div class="tab-pane" id="<?php echo $count ?>">
<?php echo $content ?>
</div>

<?php } ?>
</div>

I am certain there is a more efficient way to do without a gazillion loops and queries.
Anyone have any ideas? I’m want to learn, and put code an elegant solution.

0

Leave a Comment