• Sam

    (@forbiddenchunk)


    I have been trying to get child and grandchild pages on the parent page which I’ve managed but now when I go to the child or grandchild page it either doesn’t show or only shows the child pages…

    – Parent Page
    — Child Page
    — Grandchild Page

    This is what I’ve got so far;

    <?php
    	$pages = get_posts(array(
    	    'post_type' => 'page',
    	    'orderby' => 'title',
    	    'order' => 'ASC',
    	    'posts_per_page' => -1
    	));
    	global $post;
    ?>
    <?php $children = get_page_children($post->ID, $pages); ?>
        <?php foreach ($children as $child): ?>
    	<div class="accordion">
            <?php if ($child->post_parent == $post->ID): ?>
            <div class="parent parent--children">
            	<a href="<?php echo get_permalink($child->ID); ?>"><?php echo $child->post_title; ?></a>
            	<div class="dropdown_btn"></div>
            	<?php $granchildren = get_page_children($child->ID, $pages); ?>
                <ul class="parent parent--grandchildren">
            	<?php foreach ($granchildren as $grandchild): ?>
                	<li>
                		<a href="<?php echo get_permalink($grandchild->ID); ?>"><?php echo $grandchild->post_title; ?></a>
                	</li>
            	<?php endforeach; ?>
                </ul>
        	</div>
        <?php endif; ?>
    	</div>
    <?php endforeach; ?>

    If anyone can help please, that would be great!

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You don’t show any code that outputs the parent page, so when requesting a child, it becomes the ‘parent’ of any descendants. You always need the parent page output.

    I put your code on my site’s page template as a quick test and it appears to work correctly. When following any child link, its descendants are all listed, as is the child as the new ‘parent’. Of course, my template already has code to output parent page content. I expect yours does as well, even if not listed here.

    If pages are not showing up for you, there must be other code influencing the get_posts() query, such that not all pages are returned.

Viewing 1 replies (of 1 total)
  • The topic ‘Show child pages on grandchild’ is closed to new replies.