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

Php 查询帖子输出新闻项目5次,php,wordpress,categories,posts,Php,Wordpress,Categories,Posts,我在将帖子重新发送到页面时遇到问题。我尝试获取特定类别的帖子,但当帖子被加载时,会显示5次。我试图更改管理员面板中要查看的帖子数量,但这对帖子的输出没有影响 这是我的代码: <div id="primary" class="content-area"> <main id="main" class="site-main" role="main"> <?php query_posts( array ( 'category_na

我在将帖子重新发送到页面时遇到问题。我尝试获取特定类别的帖子,但当帖子被加载时,会显示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 -->


提前谢谢

试试看:

$args数组('category\u name'=>nieuwsitem','posts\u per\u page'=>20)

$myposts=get_posts($args)

并删除:

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

结束时

我认为你可以用两个查询来查询你的帖子,所以每次你收到一篇帖子时,都会使用以下内容: while(have_posts()):the_post()


您可以使用:$myposts=get_posts(“”)再次查询帖子

您已经创建了两个循环。删除代码的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\u posts
get\u posts
,而
WP\u query
在这里更合适

您获取每篇文章的多个副本的原因是,您修改了主查询以从该类别获取文章,然后在循环中从该查询中
获取文章
,并通过
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 -->


对此,您应该使用单个
WP\u查询
,而不是
Query\u posts
get\u posts
get_posts
不需要使用循环。这里您正在修改默认查询,在这些帖子中循环(
while
),并在每个循环中运行另一个
foreach
循环,该循环输出每个帖子,这就是您获得此行为的原因。只需使用一个新的
WP\u查询
和一个简单、正常的WordPress循环即可。有关
WP\u查询
的实现,请参阅我的答案。