【问题标题】:Wordpress WP_Query posts_per_page not returning correct number of postsWordpress WP_Query posts_per_page 未返回正确数量的帖子
【发布时间】:2017-02-23 17:24:48
【问题描述】:

我浏览了有关此主题的其他帖子,但找不到任何原因说明我的代码表现如何。我正在循环浏览我的类别列表并从每个类别中选择一个最新的帖子。我遇到的问题是,即使我使用的是 'posts_per_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(); ?>

例子:

我有一个名为 How To 的类别,我应该返回该类别的最新帖子,但上面的代码显示了已检查该类别的两个帖子。

编辑: 以下是 $categories 请求的结果:

Array ( [0] => WP_Term Object ( [term_id] => 3 [name] => Events [slug] => events [term_group] => 0 [term_taxonomy_id] => 3 [taxonomy] => category [description] => [parent] => 0 [count] => 1 [filter] => raw [cat_ID] => 3 [category_count] => 1 [category_description] => [cat_name] => Events [category_nicename] => events [category_parent] => 0 ) [1] => WP_Term Object ( [term_id] => 10 [name] => How To [slug] => how-to [term_group] => 0 [term_taxonomy_id] => 10 [taxonomy] => category [description] => [parent] => 0 [count] => 2 [filter] => raw [cat_ID] => 10 [category_count] => 2 [category_description] => [cat_name] => How To [category_nicename] => how-to [category_parent] => 0 ) [2] => WP_Term Object ( [term_id] => 4 [name] => Interviews [slug] => interviews [term_group] => 0 [term_taxonomy_id] => 4 [taxonomy] => category [description] => [parent] => 0 [count] => 2 [filter] => raw [cat_ID] => 4 [category_count] => 2 [category_description] => [cat_name] => Interviews [category_nicename] => interviews [category_parent] => 0 ) [4] => WP_Term Object ( [term_id] => 2 [name] => People [slug] => people [term_group] => 0 [term_taxonomy_id] => 2 [taxonomy] => category [description] => [parent] => 0 [count] => 1 [filter] => raw [cat_ID] => 2 [category_count] => 1 [category_description] => [cat_name] => People [category_nicename] => people [category_parent] => 0 ) [6] => WP_Term Object ( [term_id] => 5 [name] => Results [slug] => results [term_group] => 0 [term_taxonomy_id] => 5 [taxonomy] => category [description] => [parent] => 0 [count] => 1 [filter] => raw [cat_ID] => 5 [category_count] => 1 [category_description] => [cat_name] => Results [category_nicename] => results [category_parent] => 0 ) )

【问题讨论】:

  • 尝试将 'ignore_sticky_posts' =&gt; 1 添加到您的 args 数组中。
  • 你能把 wp_reset_postdata() 放在 endwhile 之后吗? ?
  • 尝试了忽略置顶帖子,但得到了相同的结果。 Mohammad 你认为 wp_reset 需要进入 foreach 循环吗?
  • @MohammadAshiqueAli 尝试放置在 foreach 循环中得到了相同的结果
  • @ActionCoding 你能告诉我 $category 的结果吗...它有多少个值?

标签: php wordpress


【解决方案1】:

对于那些正在研究这个问题的人。我不得不使用下面的代码强制它只显示最新的:

<?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 检查,因此它只使用每个循环的第一次出现。这可能不是最有效的方法,所以如果有人建议让代码更高效,我会喜欢的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-24
    相关资源
    最近更新 更多