1 回答
TA贡献1802条经验 获得超6个赞
您的示例代码的主要问题是您想要返回输出,但对 的调用wp_list_pages没有返回所需的信息,而是直接回显它。如果要将 的结果添加wp_list_pages到输出中,则必须将参数添加到wp_list_pages. 根据wordpress 文档,您必须设置echo为false.
要在每个部分的div后面添加,请看下面的代码:
function render_sections( $sections ) {
$sections = explode(',', $sections);
$output = '';
$stay_put = wp_list_pages(['echo' => false);
if ( empty( $sections ) ) {
return $output;
}
foreach( $sections as $section ) {
switch ( $section ) {
case 'section_a':
$output .= "<div>Section A</div>';
$output .= $stay_put;
break;
case 'section_b':
$output .= '<div>Section B</div>';
$output .= $stay_put;
break;
default:
break;
}
}
return $output;
}
请注意,我已将函数名称从 更改为sort_sections,render_sections因为这似乎更接近于描述其功能(干净的代码)。
- 1 回答
- 0 关注
- 229 浏览
添加回答
举报