我正在尝试为我的WordPress博客类别创建自定义网站地图。为此,我在函数中添加了以下代码。php文件,当我保存它时,我的博客会变成白色。什么都没有出现。我通过FTP删除了这段代码,然后一切又恢复正常。
现在我想制作并使用这个代码。现在有人能帮我修复这个代码吗?
/* ------------------------------------------------------------------------- *
* Custom Dynamic XML Sitemap Generator For Categories
/* ------------------------------------------------------------------------- */
add_action("publish_post", "cat_create_sitemap");
add_action("publish_page", "cat_create_sitemap");
function cat_create_sitemap() {
$categoriesForSitemap = get_categories(array(
\'hide_empty\' => 0,
depth => 0,
\'hierarchical\' => false
));
$sitemap = \'<?xml version="1.0" encoding="UTF-8"?>\';
$sitemap .= \'<?xml-stylesheet type="text/xsl" href="sitemap-style.xsl"?>\';
$sitemap .= \'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\';
foreach($categoriesForSitemap as $category) {
setup_postdata($category);
$categorydate = explode(" ", $category->category_modified);
$sitemap .= \'<url>\'.
\'<loc>\'. get_permalink($category->ID) .\'</loc>\'.
\'<priority>1</priority>\'.
\'<lastmod>\'. $categorydate[0] .\'</lastmod>\'.
\'<changefreq>daily</changefreq>\'.
\'</url>\';
}
$sitemap .= \'</urlset>\';
$fp = fopen(ABSPATH . "custom-cat-sitemap.xml", \'w\');
fwrite($fp, $sitemap);
fclose($fp);
}