0

I created an index.php file with two loops. The first one is standard and should only show the title of the page. Just below it is the second loop with class WP_Query. My problem is that the first loop takes arguments from the second one and page title is displayed several times. I added as it is written in the documentation wp_reset_postdata() at the end of each loop, but this does not solve my problem. Can any of you explain it to me and indicate a solution ?

<?php get_header(); ?>
  <main role="main">
    <section class="container">
      <div class="row">
        <div class="col-lg-8">
        <?php if ( have_posts() ) : ?>
          <?php while ( have_posts() ) : the_post(); ?>
          <h1 class="text-center><?php the_title(); ?></h1>
          <?php endwhile; ?>
        <?php endif; ?>
        <?php wp_reset_postdata(); ?>
          <div class="row">
          <?php
          $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
          $args = array('post_type'=>'post', 'post_status'=>'publish', 'paged'=>$paged );
          $post_query = new WP_Query($args); ?>
          <?php if ( $post_query->have_posts() ) : ?>
            <?php while ( $post_query->have_posts() ) : $post_query->the_post(); ?>
            <div class="col-lg-4">
              <article class="news-item">
              <?php $image = get_the_post_thumbnail_url(get_the_ID(), 'medium'); ?>
                <a class="news-img" href="<?php echo get_permalink(); ?>" style="background-image: url(<?php echo esc_url($image); ?>);">
                </a>
                <div class="news-copy">
                  <h2 class="news-title"><?php the_title(); ?></h2>
                  <?php the_excerpt(); ?>
                </div>
                <div class="news">
                  <div class="news-date"><?php echo get_the_date( 'd/m/Y' ); ?></div>
                  <a class="btn" href="<?php echo get_permalink(); ?>">
                    <?php if (function_exists('pll_e')) { pll_e('More'); } ?>
                    <i class="ti-angle-right"></i>
                  </a>
                </div>
              </article>
            </div>
            <?php endwhile; ?>
          <?php else : ?>
            <p><?php if (function_exists('pll_e')) { pll_e('Sorry, xxx...'); } ?></p>
          <?php endif; ?>
          <?php the_posts_pagination(); ?>
          <?php wp_reset_postdata(); ?>
          </div>
        </div>
      </div>
    </section>
  </main>
<?php get_footer(); ?>
1
  • The first loop looks completely separate to the second one so it can't 'take arguments from the second one' - there's no way the code can have an effect backwards in time like that. So you need to do something to debug what's going on: You could remove/comment out the entire second loop to see what the first one does on its own, ans also make sure that the presentation isn't confusing things.
    – mozboz
    Commented Jul 28, 2020 at 10:56

1 Answer 1

-1

Please try below code :

<?php get_header(); ?>
  <main role="main">
    <section class="container">
      <div class="row">
        <div class="col-lg-8">
        <?php if ( have_posts() ) : ?>
          <?php while ( have_posts() ) : the_post(); ?>
          <h1 class="text-center><?php the_title(); ?></h1>
          <?php endwhile; ?>
           <?php wp_reset_postdata(); ?>
        <?php endif; ?>
        
          <div class="row">
          <?php
          $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
          $args = array('post_type'=>'post', 'post_status'=>'publish', 'paged'=>$paged );
          $post_query = new WP_Query($args); ?>
          <?php if ( $post_query->have_posts() ) : ?>
            <?php while ( $post_query->have_posts() ) : $post_query->the_post(); ?>
            <div class="col-lg-4">
              <article class="news-item">
              <?php $image = get_the_post_thumbnail_url(get_the_ID(), 'medium'); ?>
                <a class="news-img" href="<?php echo get_permalink(); ?>" style="background-image: url(<?php echo esc_url($image); ?>);">
                </a>
                <div class="news-copy">
                  <h2 class="news-title"><?php the_title(); ?></h2>
                  <?php the_excerpt(); ?>
                </div>
                <div class="news">
                  <div class="news-date"><?php echo get_the_date( 'd/m/Y' ); ?></div>
                  <a class="btn" href="<?php echo get_permalink(); ?>">
                    <?php if (function_exists('pll_e')) { pll_e('More'); } ?>
                    <i class="ti-angle-right"></i>
                  </a>
                </div>
              </article>
            </div>
            <?php endwhile; ?>
             <?php wp_reset_postdata(); ?>
          <?php else : ?>
            <p><?php if (function_exists('pll_e')) { pll_e('Sorry, xxx...'); } ?></p>
          <?php endif; ?>
          <?php the_posts_pagination(); ?>
          
          </div>
        </div>
      </div>
    </section>
  </main>
<?php get_footer(); ?>
2
  • It would be useful to explain what you changed and why
    – mozboz
    Commented Jul 28, 2020 at 11:42
  • I have up reset post data function after while loop.
    – maulik zwt
    Commented Jul 28, 2020 at 11:46

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.