Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?
share|improve this question
2  
What is the problem? This is a page template, so you have access to the page content. By means of another separate query you gain access to a specific post, for instance, and thus can output its content. So? –  t f Mar 11 '13 at 18:25
    
Please be patient before voting down. I's struggling for it and then I found the solution. I tried to Q&A here to share the logic with others - I think it will clarify the fact in a way I's looking for it. Hope the Q & A is clear to you. –  Mayeenul Islam Mar 11 '13 at 18:41
    
Firstly, I did not downvote your question. Secondly, thanks for sharing your knowledge with us. You're absolutely right to do so. I guess, the problem is/was that this question was not that hard to solve for experienced WP users/developers, as well as the fact that you posted the question alone. If you want to question & answer right from the start, just include your answer/solution directly on the same page that you write your question on. Below the Post Your Question button there is a check box Answer your own question. Thanks again. –  t f Mar 11 '13 at 19:10
    
wp_reset_postdata() for the rescue. Should be done after each custom query. –  kaiser Mar 11 '13 at 20:06
add comment

2 Answers

up vote 1 down vote accepted

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
    if ( have_posts() ) : ?>                        
        <?php
        $my_query = new WP_Query( 'cat=1' ); // I used a category id 1 as an example
        $arg= array($my_query);                         
        ?>
        <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 -->
share|improve this answer
    
That second query shouldn't be inside if( have_posts() ) because that statement will always be true. You should call if( $my_query->have_posts() ) after the $my_query = new WP_Query( 'cat=1' ); and args lines if you want to check that query has results. –  t31os Apr 12 '13 at 8:31
add comment

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

wp_reset_query()

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.