首页 > 解决方案 > 循环浏览有和没有孩子的帖子

问题描述

我有想要循环浏览的这种帖子类型。我必须创建两个不同的部分。顶部在它循环的地方工作。但我想排除持有孩子的父母(#6474)以及该父母中的所有内容,以便在不同的行中循环。

到目前为止我所拥有的。这适用于发布所有帖子。但目前包括所有父母和孩子减去#6474。只是想让这显示那些只是父母的人。

试图弄清楚如何创建另一行,只显示帖子类型中的孩子。


$customersPage_args = array (
    'post_type'     => array( $global_cat ),
    'post_status'   => array( 'publish' ),
    'posts_per_page'  => -1,
    'order'         => 'ASC',
    'orderby'       => 'publish_date',
    'post__not_in' => array(6474) //excluding the ID holding the children
);

$global_cat_query = new WP_Query( $customers_sort ); ?>

<h3 class="h2 display <?php echo $block[className]; ?>"><?php echo $block_heading; ?></h3>
<div class="card-row">
    <div class="card u-pb-0">
        <div class="row">
        <?php // The Loop
        if ( $global_cat_query->have_posts() ) :
            while ( $global_cat_query->have_posts() ) : $global_cat_query->the_post();  ?>
                <div class="col-md-3 col-sm-4 col-6">

                    <a href="<?php echo get_permalink(); ?>">
                        <div class="card card u-mt-0 u-mb-4 align-items-center">
                            <img src="<?php echo get_the_post_thumbnail_url(); ?>" alt="<?php the_title(); ?>" />
                        </div>
                    </a>
                </div>

            <?php endwhile;
        endif;
        // Restore original Post Data
        wp_reset_postdata(); ?>
        </div>
    </div>
</div>


<?php endif; ?>

标签: phpwordpress

解决方案


如果您只想要顶级项目,则可以使用该post_parent参数。如果将其设置为0,它只会找到“父级”(又名“顶级”帖子):

$customersPage_args = array (
    'post_type'      => array( $global_cat ),
    'post_status'    => array( 'publish' ),
    'posts_per_page' => -1,
    'order'          => 'ASC',
    'orderby'        => 'publish_date',
    'post__not_in'   => array(6474), //excluding the ID holding the children
    'post_parent'    => 0, // Only get "Top Level" posts
);

推荐阅读