我正在尝试使用query\\u posts列出页面ID的所有子代。使用post\\u parent只列出第一级子代。
这张Trac票据似乎解决了这个问题:http://core.trac.wordpress.org/ticket/5742, 但我不明白——post\\u parent只列出第一级的子级。
<?php
query_posts( array(
\'post_parent\' => 58, // Only shows posts that are direct children of the Machinery page. I want all descendants.
//\'child_of\' => 58, // When used, shows posts filterd by taxonomy and term, but not filtered by child of ID 58. Acutally omits direct child of 58 but shows grandchild.
\'post_status\' => \'any\',
\'post_type\' => \'any\',
\'taxonomy\' => \'industries\',
\'term\' => \'dairy\'
));
?>
<?php if ( have_posts() ) : ?>
<h4>Dairy Machinery</h4>
<ul>
<?php while (have_posts()) : the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; wp_reset_query(); ?>
如何让query\\u帖子列出页面ID的所有子体?
SO网友:developdaly
如果你没有have 使用query_posts()
那么这是一种方法:
function my_menu() {
global $post;
if(!$post->post_parent){
// will display the subpages of this top level page
$children = wp_list_pages( array( \'title_li\' => \'\', \'child_of\' => $post->ID, \'echo\' => 0 ) );
} elseif($post->ancestors){
// diplays only the subpages of parent level
$ancestors = end($post->ancestors);
$children = wp_list_pages( array( \'title_li\' => \'\', \'child_of\' => $ancestors, \'echo\' => 0 ) );
} else {
// diplays all pages
$children = wp_list_pages( array( \'title_li\' => \'\', \'echo\' => 0 ) );
}
if ($children) {
echo \'<ul id="my-menu">\';
echo $children;
echo \'</ul>\';
}
}