查询帖子输出新闻项目5次


Query posts outputs news items 5 times

我在将帖子检索到页面中时遇到问题。我试图获得特定类别的帖子,但当帖子被加载时,它们会被显示5次。我已经准备好更改要在管理面板中查看的帖子数量,但这对帖子的输出没有影响。

这是我的代码:

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
        <?php
        query_posts( array ( 'category_name' => 'nieuwsitem', 'posts_per_page' => 20 ) );
         while ( have_posts() ) : the_post(); ?>
        <?php $myposts = get_posts('');
              foreach($myposts as $post) :
              setup_postdata($post);
              ?>
                <div class="post-item">
                  <div class="thedate"><?php echo get_the_time('d/m/Y', $post->ID); ?></p></div>
                  <div class="post-info">
                    <h2 class="post-title">
                    <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">
                    <?php the_title(); ?>
                    </a>
                    </h2>
                  </div>
                  <div class="post-content">
                  <?php the_content(); ?>
                  </div>
                </div>
              <?php endforeach; wp_reset_postdata(); ?>
        <?php endwhile; // end of the loop. ?>
    </main><!-- #main -->
</div><!-- #primary -->

提前感谢!

您已经创建了2个循环。删除代码的foreach部分应该可以修复此问题:

<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
    <?php
    query_posts( array ( 'category_name' => 'nieuwsitem', 'posts_per_page' => 20 ) );
     while ( have_posts() ) : the_post(); ?>
            <div class="post-item">
              <div class="thedate"><?php echo get_the_time('d/m/Y', $post->ID); ?></p></div>
              <div class="post-info">
                <h2 class="post-title">
                <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">
                <?php the_title(); ?>
                </a>
                </h2>
              </div>
              <div class="post-content">
              <?php the_content(); ?>
              </div>
            </div>
    <?php endwhile; // end of the loop. ?>
</main><!-- #main -->

这是正确的方法。在大多数情况下应该避免使用query_postsget_posts,而WP_Query在这里更合适。

您获得每个帖子的多个副本的原因是,您修改了主查询以从该类别中获取帖子,然后在循环中从该查询中get_posts并通过foreach显示所有帖子,因此由于嵌套循环,您将为该类别中的每个帖子输出该类别中的每个文章。get_posts不可与环路一起使用。

这应该为你做:

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
        <?php
        $catquery = new WP_Query('category_name=nieuwsitem&posts_per_page=20');
        if ($catquery->have_posts()) :
        while ( $catquery->have_posts() ) : $catquery->the_post(); ?>
                <div class="post-item">
                  <div class="thedate"><?php echo get_the_time('d/m/Y', $post->ID); ?></p></div>
                  <div class="post-info">
                    <h2 class="post-title">
                    <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">
                    <?php the_title(); ?>
                    </a>
                    </h2>
                  </div>
                  <div class="post-content">
                  <?php the_content(); ?>
                  </div>
                </div>
        <?php endwhile; // end of the loop. 
        else :
            echo '<h3>No posts found.</h3>';
        endif; ?>
    </main><!-- #main -->
</div><!-- #primary -->

尝试:

$args数组('category_name'=>'nieuwsitem','posts_per_page'=>20);

$myposts=get_posts($args);

并删除:

while(have_posts()):the_post();?>

endwhile;

我认为你用两个查询来查询你的帖子,所以每次你收到一个帖子时:while(have_posts()):the_post();

您可以使用以下命令再次查询帖子:$myposts=get_posts('');