get_pages with content along with page template

I’m trying to do a single page template. I have assigned page template to all my created pages. The problem is how do I get the page template content?

I have the following code in my index.php

Read More
    <?php
        $pages = get_pages(array('sort_column' => 'menu_order', 'sort_order' => 'ASC'));
        foreach( $pages as $page ) :
            $id = $page->ID;
    ?>

        <article id="<?= $page->post_name; ?>">
            <?php
                wp_reset_query();
                $query = new WP_Query(array('p' => $id, 'post_type' => 'page'));
                while( $query->have_posts() ) :
                    $query->the_post();
                    the_content();
                endwhile;
            ?>

        </article>
    <?php
        endforeach;
    ?>

In this way, I can only get the content that is typed in by the user but not the actual assigned page template. How do I get the page template content too?

Related posts

Leave a Reply

1 comment

  1. That wouldn’t be a good idea, as it would incorporate the HTML head (etc.) multiple times in your page. You should probably move all the extra markup in your page template to a template part (e.g. content-custom.php), and call that file with get_template_part (in your page template, as well as in your custom query in index.php):

    get_template_part( 'content', 'custom' );