奇怪的请求,但我有一个自定义的帖子类型"Proofs",其中有不同的子页面。
父级1
父级2
-Child1
-孩子2
父级3
-Child4
当我在父页面上时,我希望它列出自己的子页面。不是自定义帖子类型中的所有子项,而是只有它下面的子项。
所以当我在Parent2上时,我只看到Child1和Child2被列出。看起来很简单,但我无法破解。
使用WP_Query:
<?php
$id = get_the_ID();
$q = new WP_Query(array(
'post_type'=>'proofs',
//'post__in'=>array($id), //Uncomment this if you want to include the parent
'post_parent'=>$id
));
?>
<ul>
<?php while($q->have_posts()) : $q->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
使用wp_list_pages:
<?php
$id = get_the_ID();
wp_list_pages(array(
'child_of'=>$id,
'post_type'=>'proofs'
));
?>
wp_list_pages是一种简单得多的方法,但在不使用自己的Walker的情况下,您对它如何呈现标记的控制较少。如果您需要根据需要轻松地定制输出,我的建议是使用WP_Query。否则,如果您只是在查找链接列表,wp_list_pages就可以了。