【发布时间】:2020-09-28 14:49:08
【问题描述】:
我有一个 WP 短代码给我带来了问题。
基本上,短代码只是使用几个参数从另一个帖子中提取内容。然后它会加载一个部分模板。
保存包含短代码的页面时,WP Admin 中会出现问题。保存页面更新时确实保存正确,但结果页面是输出短代码内容的页面。
我在 get_template_part() 周围使用输出缓冲有两个原因: 1. 所以我的代码中只有一个模板实例 - 和 - 2. 因为模板实际上非常重要,并将其全部附加到输出变量将是一项艰巨的任务。
除了保存页面时,短代码在各个方面都可以正常工作。
这是一个演示该问题的视频:
https://www.awesomescreenshot.com/video/1146323?key=103ae00d841b47cee8a902eb18c8988a
这是我的代码:
function get_main_page_content( $atts ) {
$main_page_id = $atts['main_page_id'];
$section = $atts['section'];
$people_display_option = $atts['people_display_option'];
$GLOBALS['sc_display_option'] = $people_display_option;
ob_start();
if(have_rows('flexible_content', $main_page_id)):
while(have_rows('flexible_content', $main_page_id)): the_row();
if ( $section == 'agenda' ) {
get_template_part('partials/agenda');
}
if ( $section == 'people_cards' ) {
get_template_part('partials/people-cards');
}
endwhile;
endif;
ob_end_flush();
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_shortcode('get_main_page_content', 'get_main_page_content');
【问题讨论】:
-
我目前无法运行您的脚本,但在我看来,不需要 ob_end_flush() 并且是多余的。这可能会导致 OB 发送两次,从而导致屏幕上出现该代码。如果您放弃这条线,我会很好奇您的问题是否仍然存在。此外,对于您的确切用例的非常简化的版本,请查看此博客文章:konstantin.blog/2013/get_template_part-within-shortcodes
-
啊伙计,@GregBurkett!你完成了将这个输出转换为字符串的时间。感谢上帝,您比我更了解输出缓冲。如果您将此复制到答案中,我一定会给予您信任。感谢帮助!
标签: wordpress buffer shortcode