我试图使用do\\u shortcode()显示数组返回的项目列表。问题是我必须将echo语句与do\\u shortcode一起使用,而在我的函数中,我必须使用foreach循环来准备我希望使用do\\u shortcode()显示的项目数组。
这是代码
<?php echo do_shortcode(\'[su_accordion]\' . hsj_accordion($slugs) . \'[/su_accordion]\'); ?>
<?php function hsj_accordion($slugs) {
foreach($slugs as $slug ) {
echo do_shortcode(\'[su_spoiler title="\' . $slug . \'"]\' . hsj_product_list($slug) . \'[/su_spoiler]\');
}
} ?>
<?php function hsj_product_list( $slug ) {
$args = array(
\'post_type\' => \'product\',
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'product_cat\',
\'field\' => \'slug\',
\'terms\' => $slug
)
)
);
$products = get_posts( $args );
$product_list[] = array();
foreach( $products as $post ) {
$product = wc_get_product( $post );
echo $product->get_title();
}
} ?>