Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mutliple Wordpress loops in one page, produces same data

I'm having an issue with using more than one loop in the header.php file. The issue is that all sections display the results of the final loop and not their own. Here is my code:

        <div class="catagory active face">
          <?php /*-- Body --*/
            $args01 = array('post_type' => 'services','category' => 'face',);
            $loop01 = new WP_Query( $args01 );
            if ( $loop01->have_posts() ) :
              while ( $loop01->have_posts() ) : $loop01->the_post();
                echo '<p><a class="text-white" href="'.get_permalink().'">'.get_the_title().'</a></p>';
              endwhile; wp_reset_postdata();
            endif;
          ?>
        </div>

        <div class="catagory hide breast">
          <?php /*-- Body --*/
            $args02 = array('post_type' => 'services','category' => 'breast',);
            $loop02 = new WP_Query( $args02 );
            if ( $loop02->have_posts() ) :
              while ( $loop02->have_posts() ) : $loop02->the_post();
                echo '<p><a class="text-white" href="'.get_permalink().'">'.get_the_title().'</a></p>';
              endwhile; wp_reset_postdata();
            endif;
          ?>
        </div>

        <div class="catagory hide body">
          <?php /*-- Body --*/
            $args03 = array('post_type' => 'services','category' => 'body',);
            $loop03 = new WP_Query( $args03 );
            if ( $loop03->have_posts() ) :
              while ( $loop03->have_posts() ) : $loop03->the_post();
                echo '<p><a class="text-white" href="'.get_permalink().'">'.get_the_title().'</a></p>';
              endwhile; wp_reset_postdata();
            endif;
          ?>
        </div>

Any ideas as to why the first two loops displays the third loops result? What am I missing here?

like image 572
Charlie McShane Avatar asked Mar 12 '26 06:03

Charlie McShane


1 Answers

I think you should move wp_reset_postdata(); to after endif; in all loops

like image 142
Johannes Avatar answered Mar 13 '26 20:03

Johannes