星源 发表于 2025-4-13 21:29:59

修正总裁导航sitemap后的代码已兼顾规范性、健壮性和 SEO 最佳实践,可直接替换原件


<?php
/*
* 动态生成符合百度/Google标准的站点地图
*/
header("Content-type: text/xml; charset=UTF-8");
header('HTTP/1.1 200 OK');

echo '<?xml version="1.0" encoding="UTF-8"?>';
$DATA = DhCatFunctions_3::getInstance();
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1.0">
<!-- 首页 -->
<url>
    <loc><?php echo DHCAT_URL; ?></loc>
    <lastmod><?php echo gmdate('Y-m-d'); ?></lastmod>
    <changefreq>daily</changefreq>
    <priority>1.0</priority>
</url>

<!-- 最新站点 -->
<?php
$sites = $DATA->getLatestSites(1000);
foreach ($sites as $site) :
    $siteUrl = DHCAT_URL . 'site/' . $site['id'] . '.html';
?>
    <url>
      <loc><?php echo htmlspecialchars($siteUrl, ENT_QUOTES, 'UTF-8'); ?></loc>
      <lastmod><?php echo date('Y-m-d', $site['time']); ?></lastmod>
      <changefreq>weekly</changefreq> <!-- 降低更新频率 -->
      <priority>0.8</priority>
    </url>
<?php endforeach; ?>

<!-- 最新文章 -->
<?php
$posts = $DATA->getLatestPosts(1000);
foreach ($posts as $post) :
    $postUrl = !empty($post['url']) ? $post['url'] : DHCAT_URL . 'post/' . $post['id'];
?>
    <url>
      <loc><?php echo htmlspecialchars($postUrl, ENT_QUOTES, 'UTF-8'); ?></loc>
      <lastmod><?php echo date('Y-m-d', $post['time']); ?></lastmod>
      <changefreq>weekly</changefreq>
      <priority>0.7</priority>
    </url>
<?php endforeach; ?>

<!-- 站点分类 -->
<?php
$siteSorts = $DATA->getSiteSorts();
foreach ($siteSorts as $siteSort) :
?>
    <url>
      <loc><?php echo htmlspecialchars($siteSort['url'], ENT_QUOTES, 'UTF-8'); ?></loc>
      <lastmod><?php echo date('Y-m-d', $siteSort['time'] ?? time()); ?></lastmod> <!-- 修复变量名错误 -->
      <changefreq>monthly</changefreq>
      <priority>0.6</priority>
    </url>
<?php endforeach; ?>

<!-- 文章分类 -->
<?php
$postSorts = $DATA->getPostSorts();
foreach ($postSorts as $postSort) :
?>
    <url>
      <loc><?php echo htmlspecialchars($postSort['url'], ENT_QUOTES, 'UTF-8'); ?></loc>
      <lastmod><?php echo date('Y-m-d', $postSort['time'] ?? time()); ?></lastmod> <!-- 修复变量名错误 -->
      <changefreq>monthly</changefreq>
      <priority>0.5</priority>
    </url>
<?php endforeach; ?>
</urlset>

贵宾 发表于 2025-4-13 21:43:21

修正总裁导航sitemap后的代码已兼顾规范性、健壮性和 SEO 最佳实践,可直接替换原件

已优化总裁导航sitemap代码,符合W3C标准并强化SEO性能,更新后可直接覆盖原文件。优先级与更新频率配置合理,建议立即部署。
页: [1]
查看完整版本: 修正总裁导航sitemap后的代码已兼顾规范性、健壮性和 SEO 最佳实践,可直接替换原件