Wordpress 如何在循环中循环(显示子循环,然后显示孙子循环)
我正在尝试为WordPress开发一个查询,它将允许我只显示标题的子页面列表,然后在每个子页面标题下,显示孙子(子页面的子页面)页面标题及其内容的列表 例如,输出应该是这样的:Wordpress 如何在循环中循环(显示子循环,然后显示孙子循环),wordpress,Wordpress,我正在尝试为WordPress开发一个查询,它将允许我只显示标题的子页面列表,然后在每个子页面标题下,显示孙子(子页面的子页面)页面标题及其内容的列表 例如,输出应该是这样的: <ul> <li> <h1>Page 1</h1> <ul> <li> <h2>Child Page 1</h2> </li> <li&
<ul>
<li>
<h1>Page 1</h1>
<ul>
<li>
<h2>Child Page 1</h2>
</li>
<li>
<h2>Child Page 2</h2>
<ul>
<li>
<h3>Grandchild</h3>
<p>Hello, welcome to this grandchild page</p>
</li>
<li>
<h3>Grandchild #2</h3>
<p>Hello, welcome to this grandchild page</p>
</li>
</ul>
</li>
<li>
<h2>Child Page 3</h2>
</li>
</ul>
</li>
<li>
<h1>Page 2</h1>
</li>
</ul>
-
第1页
-
子页面1
-
子页面2
-
孙子
您好,欢迎来到这个孙子页面
-
孙女#2
您好,欢迎来到这个孙子页面
-
子页面3
-
第2页
它需要动态完成,这意味着我不想在查询中指定post ID号
我尝试过使用一个标准的WordPress查询,然后在第一个查询中嵌套第二个查询,但失败了
此外,我还尝试修改此处显示的代码:
最后,我还尝试修改此代码:
<?php if ( have_posts() ) { while ( have_posts() ) { the_post(); $thispage=$post->ID; }} ?>
<?php $childpages = query_posts('post_per_page=3&orderby=menu_order&order=asc&post_type=' . get_post_type( $post->ID ) . '&post_parent='.$thispage);
if($childpages){ /* display the children content */
foreach ($childpages as $post) :
setup_postdata($post); ?>
<li><a class="" href="#<?php echo($post->post_name) ?>">
<?php the_title(); ?>
</a></li>
<?php
endforeach;
} ?>
我已经试着让它工作了一天多了,我真的在兜圈子
非常感谢您的帮助。这应该对您有用,它只深入了1层,但您应该了解要点
echo "<ul>";
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
echo "<li><h1>".get_the_title()."</h1>";
$args=array(
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => 3,
'post_type' => get_post_type( $post->ID ),
'post_parent' => $post->ID
);
$childpages = new WP_Query($args);
if($childpages->post_count > 0) { /* display the children content */
echo "<ul>";
while ($childpages->have_posts()) {
$childpages->the_post();
echo "<li><h2>".get_the_title()."</h2></li>";
}
echo "</ul>";
}
wp_reset_query();
echo "</li>";
}
}
echo "</ul>";
echo“”;
if(have_posts()){
while(have_posts()){
_post();
回显“- ”。获取标题();
$args=数组(
'orderby'=>'菜单\u顺序',
“订单”=>“ASC”,
“每页帖子数”=>3,
“post\u type”=>get\u post\u type($post->ID),
“post\u parent”=>$post->ID
);
$childpages=新的WP\u查询($args);
如果($childpages->post_count>0){/*显示子内容*/
回声“
”;
而($childpages->have_posts()){
$childpages->the_post();
回显“- ”。获取标题();
}
回声“
”;
}
wp_reset_query();
回声“ ”;
}
}
回声“
”;
我尝试了某种方法,并深入了几个层次
Page 1
|
|-Child 1
|
|-Child 2
|
|-Grand child 1
|
|-Grand child 2
|
|-Great grand child 1
|
|-Great grand child 2
第1页
|
|-儿童1
|
|-儿童2
|
|-孙女1
|
|-孙子2
|
|-大孙子1
|
|-大孙子2
完整的代码在这个链接上
为了让它更深入一层,我会在重置查询之前创建另一个$args=array等,还是……?是的,基本上是从$args=array复制/粘贴(到wp\u reset\u query();在echo“