如何列出子页的子页

时间:2016-08-17 作者:Terrell Anderson

我想知道如何在wordpress中列出子页面中的子页面。

这是我设置的。

Videos (Parent)
 -Music Videos(child)
  --Video 1 (Child of Child)
  --Video 2 (Child of Child)

 -Performances (Child)
  --Video 1 (Child Of Child)
  --Video 2 (Child Of Child)
我想列出父页面“视频”中的所有“孩子的孩子”。

我知道这段代码只列出了“child”页面,但没有列出childs的子页面。

<?php
$child_pages = $wpdb->get_results("SELECT *    FROM $wpdb->posts WHERE post_parent = ".$post->ID."    AND post_type = \'page\' ORDER BY menu_order", \'OBJECT\');    ?>
<?php if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild ); ?>
<div class="child-thumb">
<?php echo get_the_post_thumbnail($page->ID, \'thumbnail\'); ?>
<a href="<?php echo  get_permalink($pageChild->ID); ?>" rel="bookmark" title="<?php echo $pageChild->post_title; ?>"><?php echo $pageChild->post_title; ?></a>
</div>
<?php endforeach; endif;
?>
我想这样做,而不必向函数中添加任何内容。php

要实现这一点,我需要补充什么。我在父页面下有五个子页面。

1 个回复
最合适的回答,由SO网友:Krzysztof Grabania 整理而成

首先,为什么要通过自定义查询查询帖子?我们有WP_Query 类别和get_posts 函数来执行此操作。

$child_pages = $wpdb->get_results("SELECT *    FROM $wpdb->posts WHERE post_parent = ".$post->ID."    AND post_type = \'page\' ORDER BY menu_order", \'OBJECT\');
成为

$pages = get_posts(array(
    \'post_type\' => \'page\',
    \'orderby\' => \'menu_order\'
    \'order\' => \'ASC\'
    \'posts_per_page\' => -1,
));
接下来,您应该使用get_page_children 功能(get_page_children on codex) 生成树列表。get_page_children 每次我们要生成子项列表时,都不会查询数据库,而是使用存储在中的所有帖子的列表$pages 变量

<ul>
    <?php foreach ($pages as $page): ?>
        <?php if ($page->post_parent == 0): ?>
            <li>
                <?php echo $page->post_title; ?>
                <?php $children = get_page_children($page->ID, $pages); ?>
                <ul>
                    <?php foreach ($children as $child): ?>
                        <?php if ($child->post_parent == $page->ID): ?>
                            <li>
                                <?php echo $child->post_title; ?>
                                <?php $granchildren = get_page_children($child->ID, $pages); ?>
                                <ul>
                                    <?php foreach ($granchildren as $grandchild): ?>
                                        <li><?php echo $grandchild->post_title; ?></li>
                                    <?php endforeach; ?>
                                </ul>
                            </li>
                        <?php endif; ?>
                    <?php endforeach; ?>
                </ul>
            </li>
        <?php endif; ?>
    <?php endforeach ?>
</ul>
编辑。我认为这符合您现在的期望:

<?php $posts = get_posts(array(\'posts_per_page\' => -1, \'post_type\' => \'page\')); ?>
<?php $children = get_page_children($post->ID, $posts); ?>
<?php foreach ($children as $child): ?>
    <?php if ($child->post_parent == $post->ID): ?>
        <div>
            <h2><?php echo $child->post_title; ?></h2>
            <ul>
                <?php $grandchildren = get_page_children($child->ID, $posts); ?>
                <?php foreach ($grandchildren as $grandchild): ?>
                    <li><?php echo $grandchild->post_title; ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>
<?php endforeach; ?>

enter image description here