【问题标题】:WP php include with shortcode is always on top带有简码的 WP php 始终位于首位
【发布时间】:2016-10-07 09:11:56
【问题描述】:

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

【问题讨论】:

    标签: php wordpress shortcode


    【解决方案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() 结束并返回缓冲区内容。

    【讨论】:

    • 工作!!!!!!非常感谢。如果没有帮助,我永远猜不到。
    • 欢迎您。当一切正常时,请考虑接受我的回答:)
    猜你喜欢
    • 2014-11-08
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 2018-02-14
    • 2020-04-25
    • 2019-08-09
    • 2015-03-14
    相关资源
    最近更新 更多