簡體   English   中英

Wordpress WP_Query posts_per_page沒有返回正確的帖子數

[英]Wordpress WP_Query posts_per_page not returning correct number of posts

我瀏覽了有關該主題的其他文章,找不到任何原因說明我的代碼的行為。 我正在瀏覽我的類別列表,並從每個類別中選擇一個最新的帖子。 我遇到的問題是,即使我使用的是'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(); ?>

例:

我有一個名為“操作方法”的類別,應該從該類別返回最新帖子,但是上面的代碼顯示了選中該類別的兩個帖子。

編輯:這是$ 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 ) )

對於那些正在尋找這個問題的人。 我不得不通過使用以下代碼來強制它僅顯示最新的代碼:

<?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檢查,因此它僅在每個循環中使用第一個匹配項。 這可能不是最有效的方法,因此,如果有人對提高代碼效率提出建議,我會喜歡的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM