我需要列出子页面,给定父页面的id。我做了如下尝试:
if ($children = get_pages(array(\'child_of\' => $post->ID)) && !empty($children)) {
//Do stuff here
}
但无论如何,这都返回false。我也试过了get_page_children($post->ID, array)
, 但我想我并没有真正理解这个函数是如何工作的。
那么,有可能得到一页的子级吗?非常感谢。
我需要列出子页面,给定父页面的id。我做了如下尝试:
if ($children = get_pages(array(\'child_of\' => $post->ID)) && !empty($children)) {
//Do stuff here
}
但无论如何,这都返回false。我也试过了get_page_children($post->ID, array)
, 但我想我并没有真正理解这个函数是如何工作的。
那么,有可能得到一页的子级吗?非常感谢。
WP codex中的示例get_page_children
通过标题为“Portfolio”的页面实现您想要的功能:
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array(\'post_type\' => \'page\', \'posts_per_page\' => \'-1\'));
// Get the page as an Object
$portfolio = get_page_by_title(\'Portfolio\');
// Filter through all pages and find Portfolio\'s children
$portfolio_children = get_page_children( $portfolio->ID, $all_wp_pages );
第一部分初始化一个新的WP\\u查询,然后使用它查询所有页面并将其作为设置为$all_wp_pages
变量get_page_children
需要两个参数:父页的ID和要在其中查找该页的子页的页对象列表($all_wp_pages
变量)。
为了使示例起作用,您需要查询创建的对象列表($all_wp_pages
) 作为get_page_children
doesn\'t query the DB, but rather checks against that list.
所以get_page_children($post->ID, $all_wp_pages)
全文:
$page_id = $post->ID;
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array(\'post_type\' => \'page\', \'posts_per_page\' => \'-1\'));
$the_pages_children = get_page_children( $page_id, $all_wp_pages );
我构建了一个单页主题,并使用get_pages() 加载所有页面的步骤front-pages.php, 但我无法加载带有自定义帖子类型的页面。这是我的密码front-pages.php:$pages = get_pages( array( \'sort_order\' => \'ASC\', \'sort_column\' => \'menu_order\' ) ); foreach ( $pages as $page_data ) {&