包含短代码的WP php总是在顶部

时间:2016-10-07 09:11:56

标签: php wordpress shortcode

我想在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');

1 个答案:

答案 0 :(得分:1)

试试这个:

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()结束它并返回缓冲区内容。

相关问题