Wordpress 如何显示具有 WP 查询-父-> 子关系的页面层次结构
Wordpress how to display hierarchy of pages with a WP Query-Parent->Child Relationship
我目前正在尝试从不同的 post 类型中获取我的所有页面,并使用 WP 查询将它们显示在单个页面上,但我想在 HTML 而不是所有这些都作为一个大列表出现,当然我不完全确定这是否可能
Parent
Child
Parent
Child
Parent
Parent
Parent
这是我目前使用的
$args = array(
'post_type'=> 'page',
'orderby' => 'title',
'post_status' => 'publish',
'order' => 'ASC',
'posts_per_page' => 20,
);
$result = new WP_Query($args);
?>
<section>
<h2><?php echo $post_type; ?></h2>
<?php
while ($result->have_posts()) {
$result->the_post();
$post = get_post();
?>
<ul>
<li class="">
<a href=""><?php echo the_title(); ?></a>
</li>
</ul>
<?php
}
wp_reset_postdata();
?>
</section>
<?php }
我尝试使用 wp_list_pages
,但我不确定如何将其用于不同的 post 类型,而且我希望每页限制 post 不显示所有内容
WP_Query
不是完成这项工作的正确工具!
Wordpress 具有满足您的需求的特殊功能:
"I tried using wp_list_pages but I'm not sure how to use that for different post types"
嗯,这就是文档在这里的原因!
例如,wp_list_pages
接受一个名为 post_type
的参数,它可以让您指定您感兴趣的“post 类型”,其默认值为 wordpress 页面“ 类型。如果您想将其更改为您的自定义 post 类型,那么您可以使用如下内容:
$args = array(
'post_type' => 'your_custom_post_type'
);
wp_list_pages($args);
"also I would like to have limited posts per page not display everything"
同样,有相关文档!
例如,如果您使用 get_pages
,则有一个参数用于限制页面上显示的页数,称为 number
。 number
参数是一个整数,表示到 return 的页数,其默认值为 0
(所有页)。
使用 get_pages
您还可以指定要查询的自定义 post 类型。该参数称为 post_type
,其默认值为 wordpress“页面”类型。
$args = array(
'post_type' => 'your_custom_post_type',
'number' => 20
);
$your_pages = get_pages($args);
foreach ($your_pages as $page) {
echo $page->post_title;
}
如果你真的need/want使用WP_Qery
那么你可以这样做:
WP_Qery
+ get_pages
$args = array(
'post_type' => 'page',
'orderby' => 'title',
'post_status' => 'publish',
'order' => 'ASC',
'posts_per_page' => 20,
);
$results = new WP_Query($args);
if ($results) {
while ($results->have_posts()) {
$results->the_post();
echo "<h3>" . get_the_title() . "<h3>";
$sub_pages_args = array(
'child_of' => get_the_ID(),
);
$sub_pages = get_pages($sub_pages_args);
echo "<ul>";
foreach ($sub_pages as $sub_page) {
echo "<li>";
echo $sub_page->post_title;
echo "</li>";
}
echo "</ul>";
}
}
wp_reset_postdata();
这将输出:
Parent Title
Child Title
Parent Title
Child Title
Parent Title
Parent Title
Parent Title
经过全面测试并适用于 wordpress 5.8
。
我目前正在尝试从不同的 post 类型中获取我的所有页面,并使用 WP 查询将它们显示在单个页面上,但我想在 HTML 而不是所有这些都作为一个大列表出现,当然我不完全确定这是否可能
Parent
Child
Parent
Child
Parent
Parent
Parent
这是我目前使用的
$args = array(
'post_type'=> 'page',
'orderby' => 'title',
'post_status' => 'publish',
'order' => 'ASC',
'posts_per_page' => 20,
);
$result = new WP_Query($args);
?>
<section>
<h2><?php echo $post_type; ?></h2>
<?php
while ($result->have_posts()) {
$result->the_post();
$post = get_post();
?>
<ul>
<li class="">
<a href=""><?php echo the_title(); ?></a>
</li>
</ul>
<?php
}
wp_reset_postdata();
?>
</section>
<?php }
我尝试使用 wp_list_pages
,但我不确定如何将其用于不同的 post 类型,而且我希望每页限制 post 不显示所有内容
WP_Query
不是完成这项工作的正确工具!
Wordpress 具有满足您的需求的特殊功能:
"I tried using wp_list_pages but I'm not sure how to use that for different post types"
嗯,这就是文档在这里的原因!
例如,wp_list_pages
接受一个名为 post_type
的参数,它可以让您指定您感兴趣的“post 类型”,其默认值为 wordpress 页面“ 类型。如果您想将其更改为您的自定义 post 类型,那么您可以使用如下内容:
$args = array(
'post_type' => 'your_custom_post_type'
);
wp_list_pages($args);
"also I would like to have limited posts per page not display everything"
同样,有相关文档!
例如,如果您使用 get_pages
,则有一个参数用于限制页面上显示的页数,称为 number
。 number
参数是一个整数,表示到 return 的页数,其默认值为 0
(所有页)。
使用 get_pages
您还可以指定要查询的自定义 post 类型。该参数称为 post_type
,其默认值为 wordpress“页面”类型。
$args = array(
'post_type' => 'your_custom_post_type',
'number' => 20
);
$your_pages = get_pages($args);
foreach ($your_pages as $page) {
echo $page->post_title;
}
如果你真的need/want使用WP_Qery
那么你可以这样做:
WP_Qery
+ get_pages
$args = array(
'post_type' => 'page',
'orderby' => 'title',
'post_status' => 'publish',
'order' => 'ASC',
'posts_per_page' => 20,
);
$results = new WP_Query($args);
if ($results) {
while ($results->have_posts()) {
$results->the_post();
echo "<h3>" . get_the_title() . "<h3>";
$sub_pages_args = array(
'child_of' => get_the_ID(),
);
$sub_pages = get_pages($sub_pages_args);
echo "<ul>";
foreach ($sub_pages as $sub_page) {
echo "<li>";
echo $sub_page->post_title;
echo "</li>";
}
echo "</ul>";
}
}
wp_reset_postdata();
这将输出:
Parent Title
Child Title
Parent Title
Child Title
Parent Title
Parent Title
Parent Title
经过全面测试并适用于 wordpress 5.8
。