discuz x3.5 网站生成sitemap
使用说明
<?php/* * $ 自动生成网站地图sitemap.xml * 1、discuz后台添加定时任务:后台–工具–计划任务–新增,名字随便,提交 * 2、然后编辑,任务脚本:cron_sitemap.php */if(!defined('IN_DISCUZ')) { exit('Access Denied');}$cfg_updateperi = '60'; // 协议文件更新周期的上限,单位为分钟$CHARSET = 'utf-8'; // 建议使用 utf-8 编码$batch_size = 1000; // 每次处理的记录数/******************************************自动生成网站地图****************************************************/$txtContent = '';$sitemap = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";$sitemap .= "<urlset\n";$sitemap .= "xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\"> \n";// 获取当前时间$now = time();$current_date = date("Y-m-d", $now);// 1. 文章$article_count = 0;$article_page = 0;do { $start = $article_page * $batch_size; $queryArticle = DB::query("SELECT aid FROM ".DB::table('portal_article_title')." ORDER BY aid DESC LIMIT $start, $batch_size"); $article_page++; $article_count = 0; while($article = DB::fetch($queryArticle)) { $article_count++; $link = dhtmlspecialchars("{$_G['siteurl']}article-{$article['aid']}-1.html"); // 注意静态规则 $txtContent .= $link."\n"; // 根据文章ID生成优先级,新文章优先级更高 $priority = min(1.0, max(0.5, 1.0 - ($article_page * $batch_size - $start) / 10000)); $sitemap .= "<url>\n"; $sitemap .= "<loc>$link</loc>\n"; $sitemap .= "<priority>".number_format($priority, 1)."</priority>\n"; $sitemap .= "<lastmod>$current_date</lastmod>\n"; $sitemap .= "<changefreq>weekly</changefreq>\n"; $sitemap .= "</url>\n"; }} while ($article_count >= $batch_size);// 2. 帖子$thread_count = 0;$thread_page = 0;do { $start = $thread_page * $batch_size; $queryThread = DB::query("SELECT tid FROM ".DB::table('forum_thread')." WHERE displayorder=0 ORDER BY tid DESC LIMIT $start, $batch_size"); $thread_page++; $thread_count = 0; while($thread = DB::fetch($queryThread)) { $thread_count++; $link = dhtmlspecialchars("{$_G['siteurl']}thread-{$thread['tid']}-1-1.html"); // 注意静态规则 $txtContent .= $link."\n"; // 根据帖子ID生成优先级,新帖子优先级更高 $priority = min(1.0, max(0.5, 1.0 - ($thread_page * $batch_size - $start) / 10000)); $sitemap .= "<url>\n"; $sitemap .= "<loc>$link</loc>\n"; $sitemap .= "<priority>".number_format($priority, 1)."</priority>\n"; $sitemap .= "<lastmod>$current_date</lastmod>\n"; $sitemap .= "<changefreq>daily</changefreq>\n"; // 帖子更新频率设为 daily $sitemap .= "</url>\n"; }} while ($thread_count >= $batch_size);$sitemap .= "</urlset>\n";// 写入xml文件$xml_file = DISCUZ_ROOT.'/sitemap.xml';$fp = @fopen($xml_file, 'w');if ($fp) { if (fwrite($fp, $sitemap) === false) { log_error("无法写入内容到 $xml_file"); } fclose($fp);} else { log_error("无法打开 $xml_file 进行写入");}// 写入txt文件$txt_file = DISCUZ_ROOT.'/sitemap.txt';$fopen = @fopen($txt_file, "w+");if ($fopen) { if (fwrite($fopen, $txtContent) === false) { log_error("无法写入内容到 $txt_file"); } fclose($fopen);} else { log_error("无法打开 $txt_file 进行写入");}// 简单的错误日志函数function log_error($message) { $log_file = DISCUZ_ROOT.'/sitemap_error.log'; $fp = @fopen($log_file, 'a'); if ($fp) { fwrite($fp, date('Y-m-d H:i:s') . " - $message\n"); fclose($fp); }}?>
声明:本文仅用于技术交流与合法用途,禁止用于任何违反当地法律的行为。
本文由 网络资源分享 发布,如需转载请注明出处。