
Typecho使用Sitemap插件时,因空分类/标签输出无效`<lastmod>`时间导致XML格式异常,谷歌站长工具无法抓取。解决方案是在插件Action.php文件中增加空值判断,跳过无内容的项。
在 Google Search Console 中添加 https://bbpad.com/sitemap.xml 后提示无法抓取:


Typecho 默认不自带站点地图,插件生成 sitemap,所以使用了下面的插件生成地图
Sitemap v1.0.5 - 社区维护版 https://github.com/typecho-fans/plugins/tree/master/Sitemap 根据报错定位到原因为: 某个分类或标签下没有内容,插件却读取了
modified时间,导致输出了空时间,XML 格式异常。 可能是谷歌对此解析的比较严格,必应那边就没有这个问题。
为避免“空分类 / 空标签”输出非法 <lastmod>,需要在插件中添加空值判断,跳过这些项。
可查看PR:https://github.com/typecho-fans/plugins/pull/201/files
简单的在输出 <lastmod> 之前判断 $art_rs['modified'] 是否为空,如果为空则 continue。
打开/usr/plugins/Sitemap目录,打开Action.php文件
while($cates->next()){
$art_rs = $db->fetchRow($db->select()->from('table.contents')
->join('table.relationships', 'table.contents.cid = table.relationships.cid')
->where('table.contents.status = ?', 'publish')
->where('table.relationships.mid = ?', $cates->mid)
->order('table.relationships.cid', Typecho_Db::SORT_DESC)
->limit(1));
//这里增加内容:文章的分类跳过
if (empty($art_rs['modified'])) continue;
echo "\t<url>\n";
echo "\t\t<loc>".$cates->permalink."</loc>\n";
echo "\t\t<lastmod>".date('Y-m-d',$art_rs['modified'])."</lastmod>\n";
echo "\t\t<changefreq>daily</changefreq>\n";
echo "\t\t<priority>0.5</priority>\n";
echo "\t</url>\n";
}
foreach($tags AS $tag) {
$type = $tag['type'];
$art_rt = $db->fetchRow($db->select()->from('table.contents')
->join('table.relationships', 'table.contents.cid = table.relationships.cid')
->where('table.contents.status = ?', 'publish')
->where('table.relationships.mid = ?', $tag['mid'])
->order('table.relationships.cid', Typecho_Db::SORT_DESC)
->limit(1));
//这里增加内容: 文章的标签跳过
if (empty($art_rt['modified'])) continue;
$routeExists = (NULL != Typecho_Router::get($type));
$tag['pathinfo'] = $routeExists ? Typecho_Router::url($type, $tag) : '#';
$tag['permalink'] = Typecho_Common::url($tag['pathinfo'], $options->index);
echo "\t<url>\n";
echo "\t\t<loc>".$tag['permalink']."</loc>\n";
echo "\t\t<lastmod>".date('Y-m-d',$art_rt['modified'])."</lastmod>\n";
echo "\t\t<changefreq>daily</changefreq>\n";
echo "\t\t<priority>0.5</priority>\n";
echo "\t</url>\n";
}

好久没打理博客,趁这个问题顺手发了一篇文章,希望这个记录可以帮到使用typecho的同学
推荐阅读
评论 (2)
k6upuj
9qdhzz