WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display parent title + grandchildren content on ancestor-page. (5 posts)

  1. EntarteteMuzak
    Member
    Posted 1 year ago #

    Here is what I want to do.
    Let's say I have this basic setup: an ancestor, a parent and a grandchild.
    Now, if I want to display the following on my ancestor page:
    every parent title + grandchildren title, thumbnail, content and permalink. Every parent and it's grandchildren should be nested together with a separate output for parent and grandchild.

    Example:

    Parent title
    Grandchild of that parent

    Another Parent title
    Grandchild of that parent

    I would like to do this so that it is not dependent on which page ID have (e.g. I can use it in different installations of WP).

    Thoughts anyone?

  2. EntarteteMuzak
    Member
    Posted 1 year ago #

    I came across this, which works for ancestor child (parent):

    <?php
    	$pages = get_pages('child_of='.$post->ID.'&sort_order=asc&sort_column=menu_order&parent='.$post->ID);
    	foreach($pages as $page)
    	{
    	?>
    
    <?php echo $page->post_title ?>
    
    <?php
    	}
    ?>

    But what about displaying the child of each parent within that loop?

  3. WPThemes.co.nz
    Member
    Posted 1 year ago #

    have another loop inside:

    <?php
    	$pages = get_pages('child_of='.$post->ID.'&sort_order=asc&sort_column=menu_order&parent='.$post->ID);
    	$count = 0;
    	foreach($pages as $page)
    	{
    		$content = $page->post_excerpt;
    		$count++;
    		$content = apply_filters('the_content', $content);
    	?>
    
    <?php echo $page->post_title ?>
    
    		<?php
    		/**
    		 * Second loop inside the first loop
    		 */
    		$child_pages = get_pages('child_of='.$page->ID.'&sort_order=asc&sort_column=menu_order&parent='.$page->ID);
    		foreach( $child_pages as $child_page ){
    		{
    			$content = $child_page->post_excerpt;
    			$count++;
    			$content = apply_filters('the_content', $content);
    
    			echo $child_page->post_title;
    		}
    		?>
    
    <?php
    	}
    ?>
  4. EntarteteMuzak
    Member
    Posted 1 year ago #

    Solved it!

    <?php
    	$pages = get_pages('child_of='.$post->ID.'&sort_order=asc&sort_column=menu_order&parent='.$post->ID);
    	foreach($pages as $page)
    	{
    	?>
    
    <?php echo $page->post_title ?>
    <?php
    	$pages = get_pages('child_of='.$page->ID.'&sort_order=asc&sort_column=menu_order&parent='.$page->ID);
    	foreach($pages as $page)
    	{
    		$content = $page->post_excerpt;
    		$content = apply_filters('the_content', $content);
    	?>
    
    <?php echo $content ?>
    
    	<?php
    	}
    ?>
    
    </div>
    	<?php
    	}
    ?>
  5. EntarteteMuzak
    Member
    Posted 1 year ago #

    Ha ha.
    Never saw your post.
    Thanks anyway @wpthemesnz

Topic Closed

This topic has been closed to new replies.

About this Topic