来自的交叉柱StackOverflow
我已经查看了有关此警告的多个其他问题(标题已发送…等),以及this thorough explanation, 但我并没有为我所处理的情况找到解决方案:
我有一个自定义的Wordpress REST API端点returns the output of get_template_part
. 当我打电话的时候get_template_part
通常在页面上没有警告。它仅在运行时出现register_rest_route
\'s回调this library. 错误开始于我开始发出这些请求时。我已经尝试在模板部分的内部和外部发出请求,后者在register_rest_route
\'s回调或作为init
行动img tag,我在哪里echo
-将URL作为src
使用来自外部API响应的数据。还有其他echo
调用此模板,所以我怀疑这是问题所在
Code:
内部功能。php:<?php
//the api endpoint declaration
add_action( \'rest_api_init\', function () {
register_rest_route(
\'theme/v2\', // namespace
\'/homepage\', // route
array( // options
\'methods\' => \'GET\',
\'callback\' => \'build_home\',
)
);
});
//the callback for the wordpress api
function build_home() {
// this is the external API request, using their PHP library.
// I linked the library above. The request returns an object with a bunch of data
include_once(get_template_directory()."/arena/arena.php");
$arena = new Arena();
$page = $arena->set_page();
$per = 8;
$slug = \'concepts-bo-6ajlvpqg\';
$channel = $arena->get_channel($slug, array(\'page\' => $page, \'per\' => $per));
//I\'m passing the response from the external API (in $channel) to get_template part
//get_template_part is then the output for the wordpress API request
get_template_part(\'custom-home-template\',null,array(\'channel\'=>$channel));
}
?>
模板内部零件:<?php
$channel=$args["channel"];
?>
<div id="concepts-box" class="bodycontent reg-width">
<?php foreach ($channel->contents as $item) { if($item->class=="Image"){
$img=$item->image["square"]["url"];
?>
<div class="concept-wrapper">
<!-- the line below is the one that triggers the error -->
<img lozad src="<?=$img; ?>">
</div>
<?php }} ?>
</div>
完全警告:<b>Warning</b>: Cannot modify header information - headers already sent by (output started at /filepath/wp-content/themes/theme/custom-home-template.php:63) in <b>/filepath/wp-includes/rest-api/class-wp-rest-server.php</b> on line <b>1372</b><br />