WordPress.org

Ready to get started?Download WordPress

Forums

Page title changes when using multiple loops? (6 posts)

  1. ben_allison
    Member
    Posted 4 years ago #

    I'm using two loops on the same page. No big deal.

    But now my tag, "<?php the_title(); ?>" doesn't show the page of the title. Instead, it shows the title of the last post being displayed.

    For fun, I tried moving the tag to before the first loop, and the title went back to normal. It seems like some variable somewhere is not getting cleared or reset...

    Ideas?

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    What are you using for you 2 Loops - get_posts or query_posts?

  3. ben_allison
    Member
    Posted 4 years ago #

    query_posts. Here are the two loops:

    This one gets Post Thumbnails (I'm using 2.9 beta)

    <?php query_posts('showposts=5'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <li>
    <?php the_post_image('full'); ?>
    </li>
    <?php endwhile;?>

    This one for excerpts

    <?php query_posts('showposts=5'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <li><a href="<?php the_permalink() ?>"><h2><?php the_title(); ?> | <?php the_time('m.j.y') ?></h2><?php the_excerpt(); ?></a></li>
    <?php endwhile;?>

    So with this code, I'm able to have post thumbnails in one div, excerpts in another.

  4. ben_allison
    Member
    Posted 4 years ago #

    Bump for an answer!

  5. ben_allison
    Member
    Posted 4 years ago #

    The problem even happens when using one loop on a page, but where the the_title tags comes well after the end of the loop, and this time, I'm using a secondary loop.

    <?php
    	$childProjects = new WP_Query();
    	$childProjects->query('post_type=pagepost_parent='.$post->ID);
    ?>
    <?php
    	while ($childProjects->have_posts()) : $childProjects->the_post();
    ?>
    <li>
    <?php
    	get_the_image( array('default_size' => 'full', 'link_to_post' => false) ); ?>
    </li>
    <?php endwhile;?>
    
    <h1><?php the_title(); ?></h1>
  6. ben_allison
    Member
    Posted 4 years ago #

    lol

    THANKS EVERYONE!

    I found the answer. I had to reset the queue, to make sure it was dead.

    <?php wp_reset_query(); ?>

    This goes for other things too, like if you're calling a post_thumbnail for the page after a have a loop that is collecting posts. The post_thumbnail for the last image will be called instead, unless you reset the query.

Topic Closed

This topic has been closed to new replies.

About this Topic