列出当前帖子类型页面的孩子

时间:2013-11-25 19:23:12

标签: php wordpress loops

奇怪的请求,但我有一个自定义帖子类型“证明”,其中包含不同的子页面。

家长1 家长2    -Child1
   -Child 2
家长3
   -Child4

当我在父母页面上时,我希望它列出自己的孩子。并非自定义帖子中的所有子项都有类型,但只有下面的子项。

因此,当我在Parent2上时,我只看到列出了Child1和Child2。看起来很简单,但我不能破解它。

1 个答案:

答案 0 :(得分:0)

使用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就可以了。