Php Wordpress WP_查询每页的帖子数不正确

Php Wordpress WP_查询每页的帖子数不正确,php,wordpress,Php,Wordpress,我已经浏览了关于这个主题的其他帖子,但找不到任何原因来解释我的代码的行为。我在我的分类列表中循环,从每个分类中选择一篇最新的文章。我遇到的问题是,即使我使用'posts\u per\u page'=>1,代码仍会返回类别中的每个帖子。下面是我正在使用的代码: <?php $categories = get_categories(array('exclude'=>array(1, 8))); foreach ($categories as $category) { $args

我已经浏览了关于这个主题的其他帖子,但找不到任何原因来解释我的代码的行为。我在我的分类列表中循环,从每个分类中选择一篇最新的文章。我遇到的问题是,即使我使用'posts\u per\u page'=>1,代码仍会返回类别中的每个帖子。下面是我正在使用的代码:

<?php $categories = get_categories(array('exclude'=>array(1, 8)));
foreach ($categories as $category) {
    $args = array(
        'cat' => $category->term_id,
        'posts_per_page' => 1,
        'orderby' => 'date'
    );
    $query = new WP_Query($args);
    while($query->have_posts()):$query->the_post(); ?>
        <li>
            <a href="<?php the_permalink(); ?>">
                <div class="post-cat"><span>Latest</span> <?php echo $cat>name; ?></div>
                <div class="post-image"><img src="<?php the_post_thumbnail_url('large'); ?>" alt="<?php the_title(); ?>" /></div>
                <div class="post-title"><?php the_title(); ?></div>
                <?php the_excerpt ();?>
                <div class="post-date"><?php the_time('F jS, Y'); ?></div>
            </a>
        </li>
    <?php endwhile; ?>
<?php } ?>
<?php wp_reset_postdata(); ?>

对于你们这些正在看这个问题的人。我不得不使用下面的代码强制它只显示最新版本:

<?php $categories = get_categories(array('exclude'=>array(1, 8)));
    foreach ($categories as $category) {
        $args = array(
            'category' => $category->term_id,
            'numberposts' => 1,
            'orderby'=>'date'
        );
        $query = get_posts($args);
        $i = 0;
        foreach($query as $post):setup_postdata($post); ?>
            <?php if($i == 0) { ?>
                <li>
                    <a href="<?php the_permalink(); ?>">
                        <div class="post-cat"><span>Latest</span> <?php echo $category->name; ?></div>
                        <div class="post-image"><img src="<?php the_post_thumbnail_url('large'); ?>" alt="<?php the_title(); ?>" /></div>
                        <div class="post-title"><?php the_title(); ?></div>
                        <?php the_excerpt ();?>
                        <div class="post-date"><?php the_time('F jS, Y'); ?></div>
                    </a>
                </li>
            <?php } $i++; ?>
        <?php endforeach; ?>
    <?php } ?>
    <?php wp_reset_postdata(); ?>


  • 我对它进行了一些不同的设置,并添加了$I check,因此它只对每个循环使用第一个匹配项。这可能不是最有效的方法,所以如果有人建议提高代码的效率,我会喜欢的

    尝试将
    'ignore\u sticky\u posts'=>1
    添加到args数组中。是否将wp\u reset\u postdata()放在endwhile之后?尝试忽略粘性帖子,但得到相同的结果。Mohammad你认为wp_重置需要进入foreach循环吗?@MohammadAshiqueAli试着将其放入foreach循环得到了同样的结果result@ActionCoding你能告诉我$category的结果吗。。。它有多少个值?
    <?php $categories = get_categories(array('exclude'=>array(1, 8)));
        foreach ($categories as $category) {
            $args = array(
                'category' => $category->term_id,
                'numberposts' => 1,
                'orderby'=>'date'
            );
            $query = get_posts($args);
            $i = 0;
            foreach($query as $post):setup_postdata($post); ?>
                <?php if($i == 0) { ?>
                    <li>
                        <a href="<?php the_permalink(); ?>">
                            <div class="post-cat"><span>Latest</span> <?php echo $category->name; ?></div>
                            <div class="post-image"><img src="<?php the_post_thumbnail_url('large'); ?>" alt="<?php the_title(); ?>" /></div>
                            <div class="post-title"><?php the_title(); ?></div>
                            <?php the_excerpt ();?>
                            <div class="post-date"><?php the_time('F jS, Y'); ?></div>
                        </a>
                    </li>
                <?php } $i++; ?>
            <?php endforeach; ?>
        <?php } ?>
        <?php wp_reset_postdata(); ?>