Multiple Loops not correctly resetting post data
-
I am using the breadcrumbs-trail plugin to generate the breadcrumbs for my site. The breadcrumb is placed in my header, outside of the main loop. Delving into the plugin code, it uses
get_the_title()
which only works inside The Loop. For the majority of pages it works as expected but for ‘Your Profile’ it displays ‘Log Out’. I have attempted to change this to ‘wp_title()’ but the resulting output is outside of the<div></div>
for the generated breadcrumb.Therefore, I have created a second loop in ‘header.php’ as follows:
<?php while (have_posts()) : the_post(); ?> <?php if ( function_exists( 'breadcrumb_trail' ) ) { breadcrumb_trail( array( 'separator' => '»', 'before' => '»' ) ); } ?> <?php endwhile; ?> <?php rewind_posts(); ?>
This then works as expected for pages and gives the correct title for ‘Your Profile’. However, when viewing categories it runs through the loop and outputs the breadcrumbs the same number of times as there are posts. To overcome this, I have done the following:
<?php query_posts('showposts=1'); while (have_posts()) : the_post(); ?> <?php if ( function_exists( 'breadcrumb_trail' ) ) { breadcrumb_trail( array( 'separator' => '»', 'before' => '»' ) ); } ?> <?php endwhile; ?> <?php wp_reset_query(); rewind_posts(); ?>
Whilst this only shows the breadcrumb once, the trail for any page or post becomes the title of the last post made and no longer shows sub-pages/categories. If I remove
rewind_posts();
there is no change, although I wouldn’t expect there to be due to thewp_reset_query()
call. If I removewp_reset_query()
and keeprewind_post()
then the text of the latest post is displayed on every page instead of the correct content. I have replacedshowposts=1
withposts_per_page=1
and the result is still the same.From this, I deduce that this loop is querying the entire database and returning the last entry overall, which clearly is not required. How should I be setting this up so that the correct page title information is output?
- The topic ‘Multiple Loops not correctly resetting post data’ is closed to new replies.