admin管理员组文章数量:1327803
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(); ?>
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(); ?>
Share
Improve this question
edited Jul 28, 2020 at 10:32
nsog8sm43x
asked Jul 28, 2020 at 10:05
nsog8sm43xnsog8sm43x
1112 bronze badges
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
Reset to default -1Please 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(); ?>
本文标签: theme developmentWhy does the first loop take arguments from the second loop
版权声明:本文标题:theme development - Why does the first loop take arguments from the second loop? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742227066a2436519.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论