Forums

get_pages how to disable grandchildren (6 posts)

  1. harrismc
    Member
    Posted 1 year ago #

    Hi guys
    im using this code to display child pages of current page
    with their content...

    <?php
    	$mypages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=asc');
    	$count = 0;
    	$numberofsub = count(get_pages('depth=1&child_of='.$post->ID));
    	foreach($mypages as $page)
    	{
    		$content = $page->post_content;
    		if($count == $numberofsub)
    			break;
    		$count++;
    	?>
    		<h2><a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a></h2>
    		<div class="entry"><?php echo $content ?></div>
    	<?php
    	}
    ?>

    The problem is , it display grandchildren too..
    How can I exclude grandchildren from being displayed ?

  2. harrismc
    Member
    Posted 1 year ago #

    I searched codex and I found only that exclude_tree can be used to hide grandchildren in conjunction with a 'child_of' value. But no details..

  3. harrismc
    Member
    Posted 1 year ago #

    Found it
    This worked
    $mypages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc&parent='.$post->ID);

  4. craigedmonds
    Member
    Posted 1 year ago #

    Found it
    This worked
    $mypages = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc&parent='.$post->ID);

    I am very grateful that you are considerate enough to post the solution.

    This has totally solved my problem also.

  5. dollypower
    Member
    Posted 1 year ago #

    Hi there,

    Thanks for sharing! Something along the same lines worked for me too. Am sharing it here, in case it is handy for others:

    // Get the page's children (but not the grandchildren)
        $children = get_pages('child_of='.$post->ID.'&parent='.$post->ID);

    Thanks x x

  6. dollypower
    Member
    Posted 1 year ago #

    Oops, the sort order should be there too

    $children = get_pages('child_of='.$post->ID.'&sort_order=desc&parent='.$post->ID);

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags