Php 循环浏览有子项和无子项的帖子

Php 循环浏览有子项和无子项的帖子,php,wordpress,Php,Wordpress,我有一个我想循环浏览的帖子类型。我必须创建两个不同的部分。顶部在循环穿过所有区域时起作用。但我想排除包含子对象(#6474)的父对象以及该父对象中的所有对象,以循环到不同的行中 到目前为止我所拥有的。这适用于发布所有帖子。但目前包括所有父母和子女,减去#6474。我只是想让你看到那些只有父母的孩子 试图找出如何创建另一行,该行只显示post类型中的子行 $customersPage_args = array ( 'post_type' => array( $global_

我有一个我想循环浏览的帖子类型。我必须创建两个不同的部分。顶部在循环穿过所有区域时起作用。但我想排除包含子对象(#6474)的父对象以及该父对象中的所有对象,以循环到不同的行中

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

试图找出如何创建另一行,该行只显示post类型中的子行


$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; ?>


$CustomerPage_args=数组(
“post_type”=>数组($global_cat),
“发布状态”=>数组(“发布”),
“每页帖子数”=>-1,
“订单”=>“ASC”,
'orderby'=>'publish_date',
'post\u not\u in'=>数组(6474)//不包括包含子项的ID
);
$global_cat_query=new WP_query($customers_sort);?>

您可以使用此功能检测帖子是否有父项:

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


为了澄清,你想显示除#6474之外的所有“顶级”帖子?是的,因为那是容纳孩子的帖子。我会检查一下,谢谢。如果我想创建一个只显示孩子的新数组?只显示
6474
的孩子?只要使用
post\u parent=>6474
。如果你有多个级别的孩子(Child>Grand Child>Great Grand Child等)您需要使用而不是标准查询。$CustomerPageChildren\u args=array('post\u type'=>array('global\u cat'),'post\u status'=>array('publish')),“每页帖子”=>-1,“订单”=>“ASC”,“订单商”=>“发布日期”,“发布父项”=>数组(6474)//仅获取“顶级”帖子);?不,只需
“发布父项”=>6474
。如果您需要帖子父项数组,您可以使用
“发布父项”=>数组(6474、123、456、/*…*/)
太棒了,谢谢。
$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
);