早在2010年,使用WordPress就出现过这个问题:每当加载RSS摘要时,Word Press都会导致浏览器出现错误。
当时,RSS提要对许多网站所有者来说相当重要,在今天,也可能仍然很重要。
最近,Word Press网站的sitemap.xml上也出现了类似的错误。 在Chrome这样的浏览器上,您在Word Press网站上工作时可能会在sitemap.xml中出现的错误如下:
This page contains the following errors:
error on line 1 at column 8: XML declaration allowed only at the start of the document
Below is a rendering of the page up to the first error.
如果出现此错误,sitemap将会出错,因此将生成一个无效的XML。 如果正在使用插件生成站点地图,那么问题好像是插件问题,但实际上并非如此。还有说是functions.php文件前后有空格,个人觉得完全没有任何关系
如何从sitemap.xml中删除这个报错误?
将一下函数放到functions.php文件的第一行
function ___wejns_wp_whitespace_fix($input) {
$allowed = false;
$found = false;
foreach (headers_list() as $header) {
if (preg_match("/^content-type:\\s+(text\\/|application\\/((xhtml|atom|rss)\\+xml|xml))/i", $header)) {
$allowed = true;
}
if (preg_match("/^content-type:\\s+/i", $header)) {
$found = true;
}
}
if ($allowed || !$found) {
return preg_replace("/\\A\\s*/m", "", $input);
} else {
return $input;
}
}
ob_start("___wejns_wp_whitespace_fix");
换句话说,它应该看起来如下:
结论
完成上述所有操作之后保存functions.php,然后可以刷新sitemap.xml。sitemap.xml现在将与Word Press生成XML和RSS。
如果按照上面修改并没有生效,请留言
原文链接:https://www.itaoda.cn/blog/893.html,转载请注明出处。
评论0