Order Displayed Child Pages (using get_template_part) on Parent page
-
Hi,
I found this great explanation: Displaying Child Page (And Template) On Parent and it works great for loading dynamically child pages to the parent page, using custom template parts for each child page.Problem I found with this, it does not order the template parts ones imported to the Parent page! Please, is there someone that could help solve this?
I will copy over part of the original post and replace it with my code:
…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 (layout_1col_visualeditor.php) create a template file (layout_content-layout_1col_visualeditor.php).
And on the custom page template (layout_1col_visualeditor.php) include the file like so:<?php /* * Template Name: Child Page - Layout 1col Visual Editor */ ?> <?php while ( have_posts() ) : the_post(); ?> <?php get_template_part( 'layout_content', 'layout_1col_visualeditor' ); ?> <?php endwhile;?>
Now you can include the get_template_part on the Parent page (layout_loader-page.php) like so:
<?php /** * Template Name: Parent Page - Section Layout Loader */ ?> <?php get_header(); ?> <!-- CONTENT WRAPPER --> <div id="content_wrapper" class="single_pages clearfix"> <?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 = ( 'layout_loader' == $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( 'layout_content-' . basename( $template_name ) , $load, $require_once ) != '' ) { $slug = pathinfo( $template_name, PATHINFO_FILENAME); } // load the content template for the child page get_template_part( 'layout_content', $slug ); } } ?> </div> <!-- END CONTENT WRAPPER --> </div> <!-- END MAIN WRAPPER --> <?php get_footer(); ?>
And just in case, I copy also the code for the included child page (layout_content-layout_1col_visualeditor.php):
<section class="visualeditor"> <!-- content block --> <main> <article class="clearfix"> <h2><?php the_title(); ?></h2> <div><?php the_content(); ?></div> </article> </main> <!-- end content block --> </section>
Thanks for your help in advance!
[please do not bump]
- The topic ‘Order Displayed Child Pages (using get_template_part) on Parent page’ is closed to new replies.