【问题标题】:A WordPress loop that displays the latest post from just 4 of your categories一个 WordPress 循环,仅显示 4 个类别的最新帖子
【发布时间】:2019-07-01 20:06:36
【问题描述】:

我是 WP 初学者,我编写了一个循环来显示我的每个类别的最新帖子,但我需要改进循环,使其仅显示 4 个不同类别的最新帖子。谁能帮我限制我循环通过的类别数量?将不胜感激。

<div class="latest-updates-container container">
    <div class="row">
        <div class="col-lg-2">
            <div class="latest-update-text">
                LATEST UPDATES
            </div>
        </div>
    </div>
    <div class="latest-updates-outter-wrapper"></div>
    <!--/.container-->
    <div class="">
        <?php

    $do_not_duplicate = array();
    $categories = get_categories();

    foreach ( $categories as $category ) {
        $args = array(
            'cat' => -2,
            'post_type' => 'post',
            'post_status' => 'publish',
            'posts_per_page' => 1,
            'post__not_in' => $do_not_duplicate,
        );
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>

        <?php while ( $query->have_posts() ) {

            $query->the_post();
            $do_not_duplicate[] = $post->ID;
    if($i % 5 == 0) { ?>

        <div class="row latest-post">

            <?php
}
?>
            <div class="col-lg-3">
                <div class="my-inner">
                    <h5 id="post-<?php the_ID(); ?>" class="blog-heading">
                        <a href="<?php the_permalink() ?>" rel="bookmark"
                            title="Permanent Link to <?php the_title(); ?>">
                            <?php the_title(); ?></a>
                    </h5>
                    <div class="time-read-now"><?php echo reading_time(); ?>&nbsp; &#183;&nbsp;
                        <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to">read now</a></h4>
                    </div>
                </div>
            </div>
            <?php $i++;
  if($i != 0 && $i % 5 == 0) { ?>
        </div>
        <!--/.row-->
        <div class="clearfix"></div>

        <?php
   } ?>

        <?php } // end while ?>

        </section>

        <?php } // end if

// Use reset to restore original query.
wp_reset_postdata();
    }
?>
    </div>

</div>
</div>

</div>

Its displaying the latest post from all categories, I need to just display 4 categories.

【问题讨论】:

    标签: php html wordpress php-7.1


    【解决方案1】:

    cat 参数允许您使用filter posts by category ID。您的代码几乎是正确的,您现在只需将类别 ID 传递给 WP_Query 类:

    <?php
    
    $do_not_duplicate = array();
    $categories = get_categories();
    
    foreach ( $categories as $category ) {
        $args = array(
            'cat' => $category->term_id, // Get posts from this category ID
            'category__not_in' => array(4, 2), // Exclude posts from categories 4 and 2
            'post_type' => 'post',
            'post_status' => 'publish',
            'posts_per_page' => 1,
            'post__not_in' => $do_not_duplicate,
        );
        $query = new WP_Query( $args );
    
        // Rest of your code here...
    

    【讨论】:

    • 嘿@cabrerahector,感谢您回复我。我已将此行添加到 WP_Query 类中,现在它显示了多个帖子。而不是删除类别 ID,我错过了什么吗?非常感谢$query = new WP_Query( array( 'cat' =&gt; '-4,-2' ) );
    • 当然,您还需要像示例中一样包含post_per_page 参数,以将查询限制为每个类别1个帖子。
    • 确保也复制其余参数。
    • 谢谢,我以为我已将它包含在我的 $args 数组中。当我尝试排除类别 4 和 2 时,它们仍然出现。任何更多的帮助将不胜感激。我一直在看这个循环几个小时,哈:)
    • 嗨 Cabrerahector,我昨晚想通了。感谢您的所有帮助
    猜你喜欢
    • 1970-01-01
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    相关资源
    最近更新 更多