我想在wordpress页面中包含一个表。表中还包括一些php代码,所以想尝试使用短代码。
我不想为此制作一个页面模板,因为我想轻松地编辑(在WP页面编辑器中)所包含的表的上下文本。
问题是,如果我包含包含表格的php文件,表格将始终在页面的顶部,而不是在文章的中间。我该如何解决这个问题?
function include_scams_page_template()
{
$path = get_template_directory() . '/templates/scams.php';
$output = include $path;
return $output ;
}
add_shortcode ('include_scams_page_template' , 'include_scams_page_template');
试试这个:
function include_scams_page_template()
{
$path = get_template_directory() . '/templates/scams.php';
ob_start();
include $path;
$output = ob_get_clean();
return $output ;
}
add_shortcode ('include_scams_page_template' , 'include_scams_page_template');
:
我猜你的代码在scams.php产生的输出来早。ob_start()
打开输出缓冲,ob_get_clean()
结束输出缓冲并返回缓冲内容。