Discuz X3.4免插件采用定时任务自动生成sitemap.xml

deer332025-03-19技术文章47

为了更好的利于SEO,加快百度、360、谷歌、bing、搜狗等收录,我们需要网站生成sitemal.xml,有些搜索引擎只支持.xml,不支持.txt,所以我们必须在511遇见论坛下自动生成sitemap.xml,由于我们采用了伪静态,所以生成的链接必须和伪静态规则一致,在discuz后台插件搜索了几个,姑且不谈收费免费,发现很不稳定,且静态规则无法兼容,这里我们采用discuz的定时任务,自动生成itemap.xml

手动建立一个cron_sitemap.php文件复制以下内容

注意自己discuz的编码,我这里utf-8,注意手动修改。

\n";
$sitemap.="\n";
 $querys = DB::query("SELECT a.tid FROM ".DB::table('forum_thread')." a inner join ".DB::table('forum_forum')." b on a.fid=b.fid ORDER BY a.tid DESC LIMIT 0,10000");
 while($threadfid = DB::fetch($querys))
 {
	 $turl=$web_root.'thread-'.$threadfid['tid'].'-1-1.html';//注意静态规则
 $link = $turl;
		$t=time();
		$riqi=date("Y-m-d",$t);
		$priority=rand(1,10)/10;
//date("D F d Y",$t);
$sitemap.="\n";
$sitemap.="$link\n";
$sitemap.="$priority\n";
$sitemap.="$riqi\n";
$sitemap.="weekly\n";
$sitemap.="\n";
 }
 $sitemap .= "\n";
 $fp = fopen(DISCUZ_ROOT.'/'.$filename,'w');
 fwrite($fp,$sitemap);
 fclose($fp);
?>

把cron_sitemap.php文件上传到论坛目录

把cron_sitemap.php上传至source\include\cron目录

discuz后台添加定时任务

后台--工具--计划任务--新增,名字随便,提交
然后编辑,任务脚本:cron_sitemap.php


discuz自动添加sitemap