How to display page content in a page template?

The question:

In my WordPress site, I made a custom page template, which contained a custom query [using WP_Query()]. With that query, I can perfectly get the posts of a certain category. But I want to show the page contents along with the queried posts.

Thing will be like:

Page Heading

page contents

Queried Post Heading

queried post contents

  • What can I do?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

I’m using two loops. First loop is to show the page content, and the second loop is to show the queried post contents. I commented into the codes where necessary. I emphasized into the loops, as Deckster0 said in WordPress support that, the_content() works only inside a WordPress Loop. I’m placing these code into a my own template:

* Template Name: My Template
get_header(); ?>

<div id="container">
    <div id="content" class="pageContent">

    <h1 class="entry-title"><?php the_title(); ?></h1> <!-- Page Title -->
    while ( have_posts() ) : the_post(); ?> <!--Because the_content() works only inside a WP Loop -->
        <div class="entry-content-page">
            <?php the_content(); ?> <!-- Page Content -->
        </div><!-- .entry-content-page -->

    endwhile; //resetting the page loop
    wp_reset_query(); //resetting the page query

        $my_query = new WP_Query( 'cat=1' ); // I used a category id 1 as an example
        <?php if ( $my_query->have_posts() ) : ?>
        <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

            <h1 class="entry-title"><?php the_title(); ?></h1> <!-- Queried Post Title -->
            <div class="entry-content">
                <?php the_excerpt(); ?> <!-- Queried Post Excerpts -->
            </div><!-- .entry-content -->

        <?php endwhile; //resetting the post loop ?>

        </div><!-- #post-<?php the_ID(); ?> -->

        wp_reset_postdata(); //resetting the post query

    </div><!-- #content -->         
</div><!-- #container -->

Method 2

Two loops is common to do this, but a bit overdosed.

Every post or page gives you the super-variable $post. Ever wondered why your get_post_meta() works with a simple $post->ID 😉 ?

So, before you start the WP_Query() that gets your listed posts, you can access the current page-/post-data with $post->ID, $post->post_content, $post->guid and so on.

In the loop, this variable gets filled by the looped post. To save it for later, you can either make a new variable

$temp_post = $post

// new WP_Query() + loop here

or call


after the listing. The last function should be called anyway to ensure that the data in your sidebar is the right for the current page/post.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment