Support » Fixing WordPress » How Can I Add Content of Child Pages to a Page

  • I am trying to display a few child pages within a single page (not the parent page). I want all the content of the child pages, titles included, to show on one page. Not a list and not excerpts. I have tried a few different php codes none of which worked.

    I tried this tutorial which worked great for parent pages but not child pages.

    And I found this on Codex

    	$mypages = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'post_date', 'sort_order' => 'desc' ) );
    	foreach( $mypages as $page ) {
    		$content = $page->post_content;
    		if ( ! $content ) // Check for empty page
    		$content = apply_filters( 'the_content', $content );
    		<h2><a href="<?php echo get_page_link( $page->ID ); ?>"><?php echo $page->post_title; ?></a></h2>
    		<div class="entry"><?php echo $content; ?></div>

    but I don’t want it to display on parent page. I need it to display on a new page.

    Also, in the above code I know this is probably a simple thing but where do I list the page id’s?

Viewing 3 replies - 1 through 3 (of 3 total)
  • I’m not sure I understand completely what you’re trying to do. Are you saying you have a parent page, say with an ID of 2, then you have some child pages of parent page 2, and you want to create a new page that shows all the child pages of parent page 2, but not the parent page’s content?

    If that’s what you’re saying, here’s what I would do. (I’m assuming you already are working with a child theme so that your changes aren’t overwritten when you update your theme.)

    1. Create a custom template by creating a file in your child theme’s wp-content folder. Call it whatever you want, say “childposts.php,” and make sure to name it according to the info in the codex link above.
    2. Copy the contents of your parent theme’s page.php into the childposts.php file.
    3. Replace the loop for posts with the code you have above.
    4. Create the new page and set the template to the custom template you created.

    You can change this line:
    $mypages = get_pages( array( 'child_of' => $post->ID, 'sort_column' => 'post_date', 'sort_order' => 'desc' ) );
    to determine which parent page’s child pages are displayed. To do that, you would just change the “$post->ID” to “2” in our example above. Alternatively, you could use the “include” or “exclude” parameters of the get_pages method instead of the:
    'child_of' => $post->ID
    to get exactly the pages you want to use.

    There is probably a better way; the drawback of this approach to me would be that you’d have to create a new custom template and hard-code the IDs for the pages you wanted to use for each new page like this you want to create.

    Yes, basically that is what I am trying to do. So I have content on 3 child pages so for example child pages are Child 1 displays all my social media buttons, Child 2-blog buttons, and Child-3 has ad buttons. Then I have a parent page that will be “View All Buttons” so I want it to show the content of the child pages so reader can view all.

    The problem I am running into is I dont have a page.php file?

    I am using the Crystal theme and I don’t see a page.php in parent genesis files either?

    I believe that’s a commercial theme, isn’t it? We’re not supposed to provide support for commercial themes here.

    You should contact the developer of the theme and ask them what file to use. They may have a better suggestion on how to accomplish what you’re trying to do, too.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How Can I Add Content of Child Pages to a Page’ is closed to new replies.