您需要使用除单个WP\\U查询之外的其他查询。你可以使用wp_list_pages()
使用定制助行器获取所需结构。
wp_list_pages
在窗口小部件、侧栏或希望列表显示的任何位置:
<ul class="cpt-hierarchy"><!-- or whatever class you like -->
<?php
wp_list_pages(array(
\'post_type\' => \'cpt\', // replace with your cpt\'s slug
\'title_li\' => \'\', // don\'t include a title LI
\'post_status\' => \'publish\', // don\'t include private/draft/etc.
\'sort_column\' => \'post_title\', // order by post title
\'walker\' => new wpse_hierarchy_walker)
);
?>
</ul>
Walker在theme的功能中。php:
<?php
class wpse_hierarchy_walker extends Walker_page {
public function start_el(&$output, $page, $depth = 0, $args = array(), $current_page = 0) {
if($depth)
$indent = str_repeat("\\t", $depth);
else
$indent = \'\';
extract($args, EXTR_SKIP);
$css_class = array(\'page_item\');
if(!empty($current_page)) {
$_current_page = get_page( $current_page );
$children = get_children(\'post_type=page&post_status=publish&post_parent=\'.$page->ID);
if(count($children) != 0) {
$css_class[] = \'hasChildren\';
}
if(isset($_current_page->ancestors) && in_array($page->ID, (array) $_current_page->ancestors))
$css_class[] = \'current_page_ancestor\';
if($page->ID == $current_page)
$css_class[] = \'current_page_item\';
elseif($_current_page && $page->ID == $_current_page->post_parent)
$css_class[] = \'current_page_parent\';
} elseif($page->ID == get_option(\'page_for_posts\')) {
$css_class[] = \'current_page_parent\';
}
$css_class = implode( \' \', apply_filters( \'page_css_class\', $css_class, $page, $depth, $args, $current_page ) );
if($page->ID == $current_page) {
$output .= $indent .\'<li class="\' . $css_class . \'">\' . $page->post_title;
} else {
$output .= $indent .\'<li class="\' . $css_class . \'"><a href="\' . get_permalink($page->ID) . \'">\' . $page->post_title .\'</a>\';
}
}
}
?>
然后使用CSS,您可以根据需要缩进子级。
li.hasChildren ul
将被孩子们包围。