WordPress.org

Ready to get started?Download WordPress

Forums

WP_query loop after single post, before comments (5 posts)

  1. nielskreijveld
    Member
    Posted 4 years ago #

    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:

    <?php
    if(have_posts()) :
    while(have_posts()) : the_post(); ?>
    // Show single post stuff
    <?php
    $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(); ?>
    <?php
    endwhile;
    endif;
    ?>

    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?

  2. nielskreijveld
    Member
    Posted 4 years ago #

    Bump, does anyone have any idea?

  3. sebastian_a
    Member
    Posted 4 years ago #

    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.

  4. kristarella
    Member
    Posted 4 years ago #

    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.

  5. sebastian_a
    Member
    Posted 4 years ago #

    I found a solution and posted it here. Maybe it helps you as well:
    http://wordpress.org/support/topic/276637?replies=2

Topic Closed

This topic has been closed to new replies.

About this Topic