You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

解决WordPress多Query Loops避免文章重复显示问题

解决WordPress模板中第三个Query Loop重复展示前两个循环文章的问题

问题根源

  1. ID收集逻辑错误:原代码仅将每个循环的最后一篇文章ID加入$ids数组,没有收集所有已展示的文章ID,导致post__not_in参数无法排除全部已展示内容。
  2. 使用不推荐的查询函数:query_posts()会干扰WordPress主查询,官方推荐使用WP_Query进行自定义查询。

修改后的完整代码

<?php
/**
 * The main template file
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 * Learn more: https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package Understrap
 */

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

get_header();

$container = get_theme_mod( 'understrap_container_type' );
?>

<?php if ( is_front_page() && is_home() ) : ?>
    <?php get_template_part( 'global-templates/hero' ); ?>
<?php endif; ?>

<div class="<?php echo esc_attr( $container ); ?>" id="content" tabindex="-1">
    <div class="row mt-5">
        <div class="col-md-12">
            <h2>Featured Posts</h2>
        </div>
    </div>
    <div class="row pt-4 pb-5">
            <?php
            $ids = array();
            $recent_posts = wp_get_recent_posts(array(
                'numberposts' => 1, // Number of recent posts thumbnails to display
                'post_status' => 'publish', // Show only the published posts
                'category' => 55 //featured posts
            ));
            foreach( $recent_posts as $post_item ) : 
                // 收集每篇展示过的文章ID
                $ids[] = $post_item['ID'];
            ?> 
                <div class="col-lg-4 mb-4 mb-lg-0">
                    <div class="card blog-post h-100 border-0 shadow-sm mb-4 overflow-hidden">
                        <a href="<?php echo get_permalink($post_item['ID']) ?>"><?php echo get_the_post_thumbnail($post_item['ID'], 'full'); ?></a>
                        <div class="card-body rounded-bottom">
                            <div class="border-bottom">
                                <p class="fw-semibold mb-2"><i class="bi bi-calendar3 es-yellow"></i> <?php echo date( 'F jS, Y', strtotime( $post_item['post_date'] ) ) ?> | <i class="bi bi-person-circle es-yellow"></i> <?php echo the_author_meta( 'display_name', $post_item['post_author'] ); ?></p>
                            </div>
                            <h4 class="card-title mt-3"><a class="text-decoration-none" href="<?php echo get_permalink($post_item['ID']) ?>"><?php echo $post_item['post_title'] ?><a/></h4>
                            <p class="card-text pb-2"><?php echo get_the_excerpt($post_item['ID']); ?></p>
                        </div>
                    </div>
                </div>
            <?php endforeach;?>
            <?php
            $recent_posts_2 = wp_get_recent_posts(array(
                'numberposts' => 3, // Number of recent posts thumbnails to display
                'offset' => 1, // Exclude the first post from the 'Recent Posts' section
                'post_status' => 'publish', // Show only the published posts
                'category' => 55 //featured posts
            ));?>
            <div class="col-lg-5">
                <ul id="" class="list-group list-group-flush">
                    <?php foreach( $recent_posts_2 as $post_item ) : 
                        // 收集每篇展示过的文章ID
                        $ids[] = $post_item['ID'];
                    ?>
                        <li class="list-group-item ps-0 pt-0 pb-3 mb-3">
                        <div class="row">
                        <div class="col-4">
                            <a href="<?php echo get_permalink($post_item['ID']) ?>"><?php echo get_the_post_thumbnail($post_item['ID'], 'thumbnail', ['class' => 'rounded']); ?></a>
                        </div>
                        <div class="col-8 align-self-center">
                            <a class="fw-bold text-decoration-none lead" href="<?php echo get_permalink($post_item['ID']) ?>">
                            <p class="mb-1"><?php echo $post_item['post_title'] ?></p>
                            </a>
                            <p class="fw-semibold"></span> <i class="bi bi-calendar3 es-yellow"></i> <?php echo date( 'F jS, Y', strtotime( $post_item['post_date'] ) ) ?> | <i class="bi bi-person-circle es-yellow"></i> <span class="pe-2"><?php echo the_author_meta( 'display_name', $post_item['post_author'] ); ?></p>
                        </div>
                        </div>
                        </li>
                    <?php endforeach;?> 
                </ul>
            </div>
        
        <div class="col-lg-3 h-1">
            <div class="card p-4 text-center">
                <h3 class="text-primary">Subscribe today</h3>
                <p class="lead fw-normal">cta here</p>
            </div>
        </div>
    </div>
</div><!-- #content --></div>
<main class="site-main bg-es-blue-100 mt-5 blog" id="main">
    <div class="container pb-5">
        <div class="row pt-5 pb-4">
            <div class="col-md-12">
                <h2 class="text-center">Explore More Topics</h2>
            </div>
        </div>
        <div class="row row-cols-1 row-cols-md-2 row-cols-lg-3 g-4">
        <?php 
        // 使用WP_Query替代query_posts
        $exclude_args = array(
            'post__not_in' => $ids,
            'post_status' => 'publish',
            'posts_per_page' => get_option('posts_per_page')
        );
        $exclude_query = new WP_Query( $exclude_args );
        
        if ( $exclude_query->have_posts() ) {
            // Start the Loop.
            while ( $exclude_query->have_posts() ) {
                $exclude_query->the_post();

                /*
                    * Include the Post-Format-specific template for the content.
                    * If you want to override this in a child theme, then include a file
                    * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                    */
                get_template_part( 'loop-templates/content-blog', get_post_format() );
            }
            // 重置查询,避免影响后续内容
            wp_reset_postdata();
        } else {
            get_template_part( 'loop-templates/content-blog', 'none' );
        }
        ?>
        <div class="w-100 d-flex justify-content-center">
            <?php
            // 基于自定义查询生成分页
            understrap_pagination( array( 'query' => $exclude_query ) );
            ?>
        </div>
    </div>
</main>
<?php
get_footer();

关键修改说明

  1. 修正ID收集:在两个foreach循环内部添加$ids[] = $post_item['ID'];,确保所有已展示的文章ID都被收集到排除数组中。
  2. 替换查询方式:用WP_Query创建自定义查询,替代query_posts(),避免破坏主查询的全局状态。
  3. 重置查询:在自定义循环结束后调用wp_reset_postdata(),恢复全局$post变量的原始状态。
  4. 适配分页:修改分页函数,传入自定义查询对象,确保分页逻辑基于排除后的结果。

内容的提问来源于stack exchange,提问作者jmspark23

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 10:35:55