php - Wordpress 短代码按父母 ID 显示所有子页面
问题描述
我正在编写一个通过父 ID 循环并显示所有子页面的短代码,但我不太确定如何制作循环并使其更加自定义。
这是我的代码: add_shortcode( 'home-page-listing', 'get_list' );
function get_list( $atts ) {
ob_start();
$atts = shortcode_atts( array(
'ids' => ''
), $atts );
if($atts['ids']!='')
{
$id_array = explode(',',$atts['ids']);
$homePages = new WP_Query( array(
'post_type' => 'page',
'post__in'=>$id_array,
'order' => 'ASC',
'orderby' => 'post__in',
'posts_per_page' => -1
) );
if ($homePages->have_posts()){?>
<div class="">
<?php while ( $homePages->have_posts() ) : $homePages->the_post(); ?>
//here's html template code
<?php endwhile;
wp_reset_postdata(); ?>
</div>
}
}
}
现在我可以[home-page-listing id=1,2,3,4]
用来显示所有选择页面 ID,但我想这样:
[home-page-listing parentID=4]
循环遍历所有子页面并显示到字体,而不是检查所有要显示的页面 id。
谢谢!
解决方案
它的简单使用post_parent
参数 WP_Query
。请检查以下示例
$homePages = new WP_Query( array(
'post_type' => 'page',
'post_parent'=>$parentID,
'order' => 'ASC',
'orderby' => 'parent',
'posts_per_page' => -1
) );
有关 WP_Query 的更多信息,请单击此处
推荐阅读
- python - 电子邮件python中没有excel文件附件
- html - 如何将深度嵌套列表缩小到窗口中剩余的空间,以便页面不滚动但列表缩小并且可以滚动
- database - 直接在数据库上执行操作(尤其是 Firestore)
- reactjs - React - 使用功能组件时如何为 .map 函数传递道具
- moodle - Moodle 文件管理器 - 关键部分
- python - 验证数字、浮点数和某些运算符的 Tkinter 条目
- android - Android Room 数据库 平均查询时间(以毫秒为单位)
- stan - 使用由正态分布和单独的多正态分布共享的参数
- docker - Docker Message":"未处理的异常:文件共享已被取消"
- python - 使用 python 从 pandas 数据框创建项集