停止包括在wordpress中放置在页面顶部

时间:2014-04-22 10:56:15

标签: php html wordpress

刚开始使用php。包括列表,使用短代码。问题是包含的列表出现在DOM中包装器的顶部,而不是我将它放在html编辑器中的位置: 函数(我放在functions.php中)是:

function objectlist() {
include("objects1.php");
}
add_shortcode('get_my_objects', 'objectlist');

html(wordpress html view):

<div class="listGenCont">
<div class="objectList1">
[get_my_objects]
</div></div>

但输出最终是:

<ul class="thingsathome"> **this is the included list**
<div class="listGenCont">
<div class="actressList">**this is where i want it**</div>
</div>

想知道为什么会发生这种情况以及如何解决这个问题

1 个答案:

答案 0 :(得分:1)

然后查看此代码段(credits):

function include_file($atts) {
    //check the input and override the default filepath NULL
    //if filepath was specified
    extract(shortcode_atts(array('filepath' => 'NULL'), $atts));

    //check if the filepath was specified and if the file exists
    if ($filepath!='NULL' && file_exists(TEMPLATEPATH.$filepath)) {
        //turno on output buffering to capture script output
        ob_start();

        //include the specified file
        include(TEMPLATEPATH.$filepath);

        //assign the file output to $content variable and clean buffer
        $content = ob_get_clean();

        return $content;
   }
}

//register the Shortcode handler
add_shortcode('include', 'include_file');

当您使用普通包含时,它会在WordPress请求开始时进行评估。此示例使用捕获输出biffer来防止这种情况。