0

so, heres the problem:

i've created a category-template (category.php) with two query loops and the main loop (in which the content from the category should get looped in). if i now click on a category the template page shows but also the first two query loops get affected by it. everything is messed up.

above is my template architecture. as you can see, i used the wp_reset_query function but this did not help. how do i make JUST THE MAIN LOOP react to the catgeory posts?

the first loop:

<div id="slider1">
 <?php $args = array( 'post_type' => 'slider');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();?>
    <div class="startslide" style="background-color: <?php echo get_post_meta($post->ID, 'Farbcode', true); ?>">
        <div class="slideimg"><?php the_post_thumbnail( 'slider' ); ?></div>
        <h1><?php the_title(); ?></h1>
        <div class="subline"><?php the_content(); ?></div>
    </div>
 <?php endwhile; ?>
 <?php wp_reset_query(); ?> 

</div> <!--! end of #slider1 -->

the second loop:

<div id="topteaser">
<?php $args = array( 'post_type' => 'teaser');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();?>
        <div class="topteaseritem" style="background:url(<?php $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 36,36 ), false, '' ); echo $src[0]; ?>) no-repeat 10px top">
            <h3><?php the_title(); ?></h3>
            <?php the_content(); ?>
        </div>
       <?php endwhile; ?>
        <?php wp_reset_query(); ?> 
</div>

..and the third loop (the main loop):

<div id="slider2">
<?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <div class="post">
        <div class="postimg"><a href="<?php the_permalink() ?>"><?php the_post_thumbnail( 'related' ); ?> </a></div>
        <div class="postcontent">
            <h3><?php the_title(); ?></h3>
            <?php the_content(); ?>
        </div>
    </div>
<?php endwhile; ?>
    <?php endif; ?>
</div> <!--! end of #slider2 -->

i hope you get my problem as i'm not a native english speaker (it's hard to explain all that) ;-)

2
  • 'but also the first two query loops get affected by it. everything is messed up.' - what does that mean? can you describe in more detail what happens in the first two loops, and what exactly 'the mess' is?
    – Michael
    Commented Nov 15, 2012 at 12:02
  • ok sorry, "messed up" menas: in the way how i did it the third loop shows all the posts from the right category (how i want it to be).. but the other first two loops do the same which they shouldn't?!?! summarized said: all three loops get affected by the catgeory-page but i want only the third loop to be affected (the other two should loop the posts from the categories which i defined by the query-codes)! Commented Nov 16, 2012 at 9:14

1 Answer 1

2

In order to provide more-specific help, we'll need to know greater detail regarding exactly what's happening that you don't expect, or what's not happening that you expect to happen, or what is happening differently from what you expect.

That said, there are at least two things that will likely help you:

  1. Use descriptive and unique variable names to hold your custom queries
  2. Include the if ( have_posts() ) part of the Loop
  3. Call wp_reset_postdata() instead of wp_reset_query()

Variable Names

You use the generic variable $arg to hold your custom query arguments, and the generic variable $loop to hold both of your custom queries. Instead, I would suggest:

$slider_query_args = array( 'post_type' => 'slider');
$slider_query = new WP_Query( $slider_query_args );

...and...

$teaser_query_args = array( 'post_type' => 'teaser');
$teaser_query = new WP_Query( $teaser_query_args );

Doing this makes your code easier to read/follow, helps ensure that you don't mix the two custom queries, and helps avoid unintended consequences in the two custom query calls.

Full Loop Call

You use:

while ( $loop->have_posts() ) : $loop->the_post();

Instead, use:

if ( $slider_query->have_posts() ) : while ( $slider_query->have_posts() ) : $slider_query->the_post();
    // Loop output
endwhile; endif;

...and...

if ( $teaser_query->have_posts() ) : while ( $teaser_query->have_posts() ) : $teaser_query->the_post();
    // Loop output
endwhile; endif;

Reset Post Data after Custom Queries

Note: This one may very likely be your main problem.

The wp_reset_query() function is intended to reset the main query after it has been altered (e.g. via query_posts()). Since you're not altering the main query, calling wp_reset_query() isn't going to do anything for you.

Instead, use wp_reset_postdata(), which is intended to reset the $post global variable, and all the related template tags (e.g. the_title(), the_content(), the_permalink(), etc.) to refer once again to the main query. Since you're calling the_post() in your custom query loops, you'll want to use wp_reset_postdata():

// Slider Loop
if ( $slider_query->have_posts() ) : while ( $slider_query->have_posts() ) : $slider_query->the_post();
    // Loop output
endwhile; endif;
// Rest postdata
wp_reset_postdata();

// Teaser Loop
if ( $teaser_query->have_posts() ) : while ( $teaser_query->have_posts() ) : $teaser_query->the_post();
    // Loop output
endwhile; endif;
// Rest postdata
wp_reset_postdata();

// Main Query Loop
if ( have_posts() ) : while ( have_posts() ): the_post();
    // Loop output
endwhile; endif;
5
  • thanks for you detailed answer but that does not work at all – it shows a blank page. the main problem is (which i apparently did not make that clear): in the way how i did it the third loop shows all the posts from the right category fine.. but the other firts two loops do the same which they shouldn't?!?! Commented Nov 16, 2012 at 9:11
  • If you got a blank page, what fatal error did you get? Commented Nov 16, 2012 at 13:11
  • nothing.. just a blank page?!? Commented Nov 16, 2012 at 16:41
  • A blank page generally means a fatal error somewhere. Ensure that WP_DEBUG is enabled, so that you can see error messages. Commented Nov 16, 2012 at 16:42
  • I don't quite get why this answer did not receive a single upvote (now it did), but there is one piece of misinformation in there. While wp_reset_query certainly is not a necessary call, if the main query hasn't been altered, it does not only reset the $wp_query global but in turn also calls wp_reset_postdata. So using the former will also end up resetting the $post global. See core.trac.wordpress.org/browser/tags/3.5.1/wp-includes/… Commented Apr 22, 2013 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.