Forums

[resolved] WP_Query Multiple Loops Displaying Page Title (not post title) on Template Pages (3 posts)

  1. pdawg51
    Member
    Posted 4 months ago #

    Hi,

    I've been trying to solve this problem for hours, and despite all the research can't seem to figure it out. However, I think the solution is probably a very simply one.

    I'm trying to use multiple loops on a template page to display the latest post from a certain category, and then below it display the titles of previous post.

    While researching I found that the article at http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action included the code I pretty much needed. And I essentially just copied it:

    <?php $my_query = new WP_Query('cat=6&posts_per_page=1');
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate = $post->ID;?>
        <?php the_title(); ?> <br/>
        <?php the_content(); ?>
    
      <?php endwhile;wp_reset_query(); ?>
    
          <?php if (have_posts()) : while (have_posts()) : the_post();
      if( $post->ID == $do_not_duplicate ) continue; ?>
    
        <?php the_title(); ?>
    
      <?php endwhile; endif; ?>

    The code works perfectly on index.php however, when I place it in a page template file, the second loop displays the title of the page it's on - and not the title of posts.

    What's going on here?

    Thanks so much for the help!

  2. alchymyth
    The Sweeper
    Posted 4 months ago #

    when I place it in a page template file, the second loop displays the title of the page it's on - and not the title of posts

    you need to add a query for the second loop.

    http://codex.wordpress.org/Function_Reference/query_posts

  3. pdawg51
    Member
    Posted 4 months ago #

    Perfect! I knew it was something simple.

    Thanks so much for your help. Saved me a lot of time!

Reply

You must log in to post.

About this Topic