我的主题是做一个页面的设计,我必须以嵌套的方式输出子页面。所以我想我只需要使用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()不能正常工作。它总是输出父页的标题。
这样的事情该怎么做才能正确?
发布于 2013-12-17 13:41:02
setup_postdata()
需要一个$post
变量:
foreach($children as $post): setup_postdata($post);
发布于 2013-12-17 13:39:32
据我所知,你有两个选择。
选项1:将hierarchical
添加到第一个查询并删除第二个查询,这样做是正确的。
选项2:在wp_reset_query();
的第一个实例下添加the_content();
我希望这能帮到你?
发布于 2013-12-17 13:59:26
<?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(),只是与内部使用的变量之间存在冲突,而这些变量以某种方式被覆盖?
https://stackoverflow.com/questions/20635440
复制相似问题