Support » Fixing WordPress » WP_query loop after single post, before comments

  • Hello people.

    I’m working on a theme that has a normal loop on the single.php page. After this loop, there is a custom WP_query loop that displays some posts from another category. Then after these posts the comments template should show up. My code is as follows:

    if(have_posts()) :
    while(have_posts()) : the_post(); ?>
    // Show single post stuff
    $my_query1 = new WP_Query('cat=6&showposts=4&orderby=rand');
    while ($my_query1->have_posts()) : $my_query1->the_post(); ?>
    // Show 4 posts from cat 6
    <?php endwhile; ?>
    <?php comments_template(); ?>

    The comments template shows up, but it is “linked” to the last post of the custom query (the last post from category 6). So when I post a comment, it actually gets posted to a different post (in category 6).
    I’ve tried adding <?php $wp_query = $temp_query; ?> before the custom loopt and <?php $temp_query = $wp_query; ?> after it, but that didn’t help.

    How do I fix this?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Bump, does anyone have any idea?

    I’d like to know this one as well. I have been struggling with this a little. I have a category of post that I would like to be added to a page after the content of that page. However I cannot get this query to run nor display.

    Even though you endwhile; on the custom loop it is still active (as you can see by the comment behaviour). You need to re-enter the normal loop.
    The only way I’ve been able to do it is by sticking
    while(have_posts()) { the_post(); }
    after the endwhile; for the custom loop.

    Perhaps if that goes askew you should even end the main loop before the custom loop and then start it again.

    I found a solution and posted it here. Maybe it helps you as well:

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘WP_query loop after single post, before comments’ is closed to new replies.