WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Displaying Child Page (And Template) On Parent (8 posts)

  1. stuntboots
    Member
    Posted 1 year ago #

    Hi all,

    Working on a template, and while I can find a lot of examples about pulling child page content/titles onto a parent, I can't seem to find a way to pull child pages wrapped in their template.

    For example, if I had a template like this (I don't, this is simplified for demonstrative purposes):

    <div class="hello">CONTENT</div>

    I know how to pull in the 'CONTENT', but I want to bring in the the whole lot. Is this possible at all? I'm sure you can test what template the child page uses, but I would like to avoid doing a series of if statements and hard-coding in the templates to the parent page.

    Any and all help is appreciated (I'd prefer not to use plugins).

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Take a look at the get_template_part function:
    http://codex.wordpress.org/Function_Reference/get_template_part

    And look how a theme as Twenty Twelve displays a page in in its page template file page.php:

    <?php while ( have_posts() ) : the_post(); ?>
      <?php get_template_part( 'content', 'page' ); ?>
      <?php comments_template( '', true ); ?>
    <?php endwhile; // end of the loop. ?>

    As you can see it uses a file called content-page.php to display the content.

    <?php get_template_part( 'content', 'page' ); ?>

    What you could do is when you create a Child page with a custom page template create another (content) template as well. You could put the whole loop in there or only the part that's inside the loop. For example for a child page with a custom page template filename "very-special-child.php" create a template file content-very-special-child.php.
    And on the custom page template ("very-special-child.php") include the file like so:

    <?php while ( have_posts() ) : the_post(); ?>
      <?php get_template_part( 'content', 'very-special-child' ); ?>
    <?php endwhile; // end of the loop. ?>

    Now you can include the get_template_part on other pages as well like so:

    <?php
    
    global $wp_query;
    
    // is Page a parent page
    if ( $post->post_parent == 0 ) {
    
    	// on a parent page, get child pages
    	$pages = get_pages( 'hierarchical=0&parent=' . $post->ID );
    
    	// loop through child pages
    	foreach ( $pages as $post ){
    
    		setup_postdata( $post );
    
    		// get the template name for the child page
    		$template_name = get_post_meta( $post->ID, '_wp_page_template', true );
    		$template_name = ( 'default' == $template_name ) ? 'page.php' : $template_name;
    
    		// default page template_part content-page.php
    		$slug = 'page';
    
    		// check if the slug exists for the child page
    		if ( locate_template( 'content-' . basename( $template_name ) , $load, $require_once ) != '' ) {
    			$slug = pathinfo( $template_name, PATHINFO_FILENAME );
    		}
    
    		// load the content template for the child page
    		get_template_part( 'content', $slug );
    	}
    }
    ?>

    I hope this makes sense :-)

  3. stuntboots
    Member
    Posted 1 year ago #

    You are beautiful! Works perfectly. The only thing I didn't do was use the prefix setup, as this was creating two files per page template. Instead, I just load the template of the page in with the post, and it seems to work exactly how I'd wanted.

    Thanks heaps for your help!

    For anyone who wants to use single files (It's practically the same as keesiemeijer's, with the 'content' parts omitted:

    <?php
    	global $wp_query;
    	// is Page a parent page
    	if ( $post->post_parent == 0 ) {
    		// on a parent page, get child pages
    		$pages = get_pages( 'hierarchical=0&parent=' . $post->ID );
    		// loop through child pages
    		foreach ( $pages as $post ){
    			setup_postdata( $post );
    			// get the template name for the child page
    			$template_name = get_post_meta( $post->ID, '_wp_page_template', true );
    			$template_name = ( 'default' == $template_name ) ? 'page.php' : $template_name;
    			// default page template_part content-page.php
    			$slug = 'page';
    			// check if the slug exists for the child page
    			if ( locate_template( basename( $template_name ) , $load, $require_once ) != '' ) {
    				$slug = pathinfo( $template_name, PATHINFO_FILENAME );
    			}
    			// load the content template for the child page
    			get_template_part( $slug );
    		}
    	}
    ?>
  4. stuntboots
    Member
    Posted 1 year ago #

    Ignore this post, an error on my behalf!

  5. Andrew Nevins
    Spam zapper & Volunteer Moderator
    Posted 1 year ago #

    Although he's still beautiful, right?

  6. stuntboots
    Member
    Posted 1 year ago #

    Endlessly.

  7. keesiemeijer
    moderator
    Posted 1 year ago #

    You guys made me lol (and it confirmed my suspicion I'm endlessly beautiful) ;)
    I'm glad you got it resolved.

  8. niknaz
    Member
    Posted 8 months ago #

    Worked perfectly.

    I concur on the endless beauty!!

Topic Closed

This topic has been closed to new replies.

About this Topic