get_pages()循环中的WordPress get_pages()?

时间:2013-12-17 13:28:04

标签: wordpress

我的主题是进行单页设计,我必须以嵌套的方式输出子页面。所以我想我可以两次使用get_pages():

<?php 
    $posts = get_pages(array(
        'sort_column' => 'menu_order',
        'parent' => 0,
    ));
    $i = 0; foreach($posts as $post): setup_postdata($post);
?>

<div class="center">

    <h1><?php the_title(); ?></h1>
    <?php the_content(); ?>

    <?php
        $children = get_pages(array(
            'sort_column' => 'menu_order',
            'parent' => $post->ID,
        ));
        foreach($children as $child): setup_postdata($child);
    ?>
        <div class="slide">
            <h1><?php the_title(); ?></h1>
            <?php the_content(); ?>
        </div>
    <?php endforeach; ?>

</div>

<?php $i++; endforeach; ?>

虽然看起来并不那么容易。第二个the_title()无法正常工作。它总是输出父页面的标题。

如何正确完成这样的事情?

3 个答案:

答案 0 :(得分:2)

setup_postdata()需要$post变量:

foreach($children as $post): setup_postdata($post);

答案 1 :(得分:1)

据我所见,您有2个选项。

选项1:通过在第一个查询中添加hierarchical并删除第二个查询,以正确的方式执行此操作。

选项2:在wp_reset_query();

的第一个实例下添加the_content();

我希望这有帮助吗?

答案 2 :(得分:0)

    <?php
        $my_query = new WP_Query(array(
            'order' => 'ASC',
            'orderby' => 'menu_order',
            'post_parent' => $post->ID,
            'post_type' => 'page',
        ));
        if($my_query->have_posts())
        { 
            while($my_query->have_posts())
            {
                $my_query->the_post();
                ?>
                <h1><?php the_title(); ?></h1>
                <?php the_content(); ?>
                <?php
            }
        }
        #wp_reset_postdata();
    ?>

使用WP_Query进行第二次查询可以正常工作。我想使用get_pages()只是与内部使用的变量发生冲突会以某种方式被覆盖?

相关问题