【发布时间】:2018-08-24 03:57:15
【问题描述】:
我正在编写一个通过父 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。
谢谢!
【问题讨论】:
标签: php wordpress loops shortcode