Discuz不用插件自动生成网站地图sitemap.xml文件
sitemap.xml网站地图可以加速百度等搜索引擎的收录,对于Discuz!来说,每天可能都会发布新的帖子和文章,如果使用手动生成站点地图文件的话,会非常的麻烦,现在教大家一个全自动生成的方法。可以同时生成sitemap.xml和sitemap.txt两种类型的文件。1、创建一个cron_sitemap.php文件,代码如下:<?php/*
* $ 自动生成网站地图sitemap.xml
* 1、discuz后台添加定时任务:后台–工具–计划任务–新增,名字随便,提交
* 2、然后编辑,任务脚本:cron_sitemap.php
*/
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}
$cfg_updateperi='60';//协议文件更新周期的上限,单位为分钟
$CHARSET='utf-8';// 选择编码方式:utf-8 or gbk
/******************************************自动生成网站地图****************************************************/
$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";
//1.文章
$queryArticle = DB::query("SELECT aid FROM ".DB::table('portal_article_title').' ORDER BY aid DESC');
while($articleaid = DB::fetch($queryArticle)){
$link = "{$_G}article-{$articleaid['aid']}-1.html";//注意静态规则
$txtContent .= $link."\n";
$t=time();
$riqi=date("Y-m-d",$t);
$priority=rand(1,10)/10;
$sitemap.="<url>\n";
$sitemap.="<loc>$link</loc>\n";
$sitemap.="<priority>$priority</priority>\n";
$sitemap.="<lastmod>$riqi</lastmod>\n";
$sitemap.="<changefreq>weekly</changefreq>\n";
$sitemap.="</url>\n";
}
//2.帖子
$queryThread = DB::query("SELECT tid FROM ".DB::table('forum_thread').' WHERE displayorder=0 ORDER BY tid DESC');
while($threadfid = DB::fetch($queryThread)){
$link = "{$_G}thread-{$threadfid['tid']}-1-1.html";//注意静态规则
$txtContent .= $link."\n";
$t=time();
$riqi=date("Y-m-d",$t);
$priority=rand(1,10)/10;
$sitemap.="<url>\n";
$sitemap.="<loc>$link</loc>\n";
$sitemap.="<priority>$priority</priority>\n";
$sitemap.="<lastmod>$riqi</lastmod>\n";
$sitemap.="<changefreq>weekly</changefreq>\n";
$sitemap.="</url>\n";
}
$sitemap .= "</urlset>\n";
//写入xml文件
$fp = fopen(DISCUZ_ROOT.'/sitemap.xml','w');
fwrite($fp,$sitemap);
fclose($fp);
//写入txt文件
$fopen = fopen(DISCUZ_ROOT.'/sitemap.txt',"w+");
fwrite($fopen,$txtContent);
fclose($fopen);
?>
2、把以上代码保存为cron_sitemap.php上传至source\include\cron目录
3、进入后台->工具->计划任务->新增
4、添加好后,记得把可用按钮勾选上
5、任务执行后,会在根目录同时生成sitemap.txt和sitemap.xml两个文件
页:
[1]