Wordpress列出带有页面内容的嵌套页面

时间:2015-04-03 05:58:47

标签: php wordpress

我正在使用此代码在Wordpress中检索嵌套的页面列表:

<?php wp_list_pages( $args ); ?>

它给出了类似于此的输出:

<ul>
    <li>Page Title 1</li>
    <li>Page Title 2
        <ul>
            <li>Page Title 3</li>
            <li>Page Title 4</li>
        </ul>
    </li>
    <li>Page Title 5</li>
</ul>

我需要这样的输出:

<ul>
    <li>Page Title 1 / Page Content 1</li>
    <li>Page Title 2 / Page Content 2
        <ul>
            <li>Page Title 3 / Page Content 3</li>
            <li>Page Title 4 / Page Content 4</li>
        </ul>
    </li>
    <li>Page Title 5 / Page Content 5</li>
</ul>

1 个答案:

答案 0 :(得分:0)

我相信您需要使用get_pages()代替,这将返回一个页面对象数组。但是,这不会为您提供设置子菜单所需的层次结构。您可以通过仅抓取顶级项目,然后遍历每个项目并检查子页面来克服这个问题:

<?php

$pages = get_pages( array(
    'sort_column' => 'menu_order',
    'parent' => 0,
));

?>

<ul>

<?php foreach( $pages as $page ): ?>
    <?php $title = $page->post_title; ?>
    <?php $content = apply_filters('the_content', $page->post_content); ?>
    <?php $children = get_pages( array( 'child_of' => $page->ID ) );?>

    <li>

    <?php echo $title . " / " . $content; ?>

    <?php if ( count( $children ) != 0 ): ?>

        <ul>

        <?php foreach( $children as $child ):
            <?php $title = $child->post_title; ?>
            <?php $content = apply_filters('the_content', $child->post_content); ?>

            <li><?php echo $title . " / " . $content; ?></li>

        <?php endforeach; ?>

        </ul>

    <?php endif; ?>

    </li>

<?php endforeach;?>

</ul>