If your goal is to load the selected custom template for each page within the loop, then you’re actually pretty close to that. You’re getting the value of _wp_page_template
, but then you’re not doing anything with that template, you’re just loading content-page.php
for each of those pages with the line get_template_part( 'content', 'page' );
.
If you instead take _wp_page_template
and pass that value to get_template_part
(minus the .php
extension), then you’ll get the behavior you describe.
if ( $page_query->have_posts() ) :
while ( $page_query->have_posts() ) :
$page_query->the_post();
$template = get_post_meta( get_the_ID(), '_wp_page_template', true );
if( $template && $post->post_content != '' ) {
get_template_part( substr( $template, 0, -4 ) );
}
endwhile;
endif;