WordPress.org

Ready to get started?Download WordPress

Forums

How Can I Add Content of Child Pages to a Page (4 posts)

  1. Murray644
    Member
    Posted 1 year ago #

    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

    <?php
    	$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
    			continue;
    
    		$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>
    	<?php
    	}
    ?>

    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?

  2. linux4me2
    Member
    Posted 1 year ago #

    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.

  3. Murray644
    Member
    Posted 1 year ago #

    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?

  4. linux4me2
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic