首页 > 解决方案 > Wordpress - 嵌套 WP_Query 循环重复内容

问题描述

我有一个嵌套的 wordpress 循环来生成包含两个不同类别内容的两列布局。发生的事情是内容重复,但显示了所有帖子。我试过使用wp_reset_query()wp_reset_postdata()没有成功。有解决办法吗?

<?php 
$main = new WP_Query(
    array(
    'post_type' => 'post',
    'category_name' => 'Notizie varie',
    'posts_per_page' => 6    
    )
);
//var_dump( $main );
    if( $main->have_posts() ): 
        while( $main->have_posts() ): $main->the_post(); 
?>
    <div class="col-8 p-4 mt-3">
        <img class="card-img-top" src="<?php echo the_post_thumbnail_url(); ?>">
        <h4 class="card-title"><?php the_title(); ?></h4>
        <p class="card-text"><?php echo get_the_excerpt( get_the_ID() ); ?></p> 
        <small class="text-muted"><?php echo get_the_date( get_the_ID() ) ?></small> 
        <small class="text-muted"><?php the_author() ?></small> 
        <a class="" href="<?php the_permalink(); ?>"><?php _e('Leggi', ''); ?></a> 
    </div>
<?php
        $side = new WP_Query(
            array(
            'post_type' => 'post',
            'category_name' => 'Secondo piano',
            'posts_per_page' => 6    
            )
        );
    if( $side->have_posts() ): 
        while( $side->have_posts() ): $side->the_post();
?>
    <div class="col-4 p-4 mt-3">
        <img class="card-img-top" src="<?php echo the_post_thumbnail_url(); ?>">
        <h4 class="card-title"><?php the_title(); ?></h4>
        <p class="card-text"><?php echo get_the_excerpt( get_the_ID() ); ?></p>
        <small class="text-muted"><?php echo get_the_date( get_the_ID() ) ?></small> 
        <small class="text-muted"><?php the_author(); ?></small> 
        <a class="" href="<?php the_permalink(); ?>"><?php _e('Leggi', ''); ?></a> 
    </div>
<?php 
        endwhile; 
        wp_reset_query();
    endif; 
        endwhile;
    endif;
        wp_reset_postdata();

标签: phpwordpressloops

解决方案


推荐阅读