有几种方法可以通过返回所有子帖子get_pages()
.
一种相当简单的方法是获取顶级父帖子的所有ID,然后将这些ID传递给exclude
的参数get_pages()
:
<?php
// Get all top-level pcct_product posts;
// The \'parent\' => 0 argument returns only
// posts with a parent ID of 0 - that is,
// top-level posts
$parent_pcct_products = get_pages( array(
\'post_type\' => \'pcct_product\',
\'parent\' => 0
) );
// Create an array to hold their IDs
$parent_pcct_product_ids = array();
// Loop through top-level pcct_product posts,
// and add their IDs to the array we just
// created.
foreach ( $parent_pcct_products as $parent_pcct_product ) {
$parent_pcct_product_ids[] = $parent_pcct_product->ID;
}
// Get all child pcct_product posts;
// passing the array of top-level posts
// to the \'exclude\' parameter
$child_pcct_products = get_pages( array(
\'post_type\' => \'pcct_product\',
\'exclude\' => $parent_pcct_product_ids,
\'hierarchical\' => false
) );
?>
(注:未测试)
编辑尝试设置hierarchical to
false“”,因为我们排除了所有顶级页面。