How to display page content in a page template?

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:

<?php
/*
* Template Name: My Template
*/
get_header(); ?>

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

    <h1 class="entry-title"><?php the_title(); ?></h1> <!-- Page Title -->
    <?php
    // TO SHOW THE PAGE CONTENTS
    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 -->

    <?php
    endwhile; //resetting the page loop
    wp_reset_query(); //resetting the page query
    ?>

    <?php
    // TO SHOW THE POST CONTENTS
    ?>                        
        <?php
        $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(); ?> -->

        <?php
        wp_reset_postdata(); //resetting the post query
        endif;
        ?>

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

Leave a Comment