sheep 发表于 2024-1-8 13:46:00

Discuz论坛自动网站地图sitemap教程(完整版)

Discuz论坛自动网站地图sitemap教程(完整版)(2步即可完成)

第一步:
在source\include\cron目录下新建文件:cron_sitemapxml_auto_sheep.php
粘贴代码:
<?php
if(!defined('IN_DISCUZ')) {
exit('Access Denied');
}

$filename='sitemap.xml';
//以下五项根据具体情况修改即可
$cfg_updateperi='60';//协议文件更新周期的上限,单位为分钟
$web_root=$_G['siteurl'];//根网址
$CHARSET='utf-8';// or gbk //选择编码方式
/***********************************************************************************************/
//网站地图sitemap.xml
$sitemap="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$sitemap.="<urlset\n";
$sitemap.="xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"\n";
$sitemap.="xmlns:mobile=\"http://www.baidu.com/schemas/sitemap-mobile/1/\"\n";
$sitemap.="xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n";
$sitemap.="xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9\n";
$sitemap.="http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">\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,50000");
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=0.8;/*rand(1,10)/10*/
$baidushipei="htmladapt";/*代码适配:htmladapt 自适应网页:pc,mobile 移动网页:mobile*/
//date("D F d Y",$t);
$sitemap.="<url>\n";
$sitemap.="<loc>$link</loc>\n";
$sitemap.="<mobile:mobile type=\"$baidushipei\"/>\n";
$sitemap.="<priority>$priority</priority>\n";
$sitemap.="<lastmod>$riqi</lastmod>\n";
$sitemap.="<changefreq>Always</changefreq>\n";
$sitemap.="</url>\n";
}
$sitemap .= "</urlset>\n";
$fp = fopen(DISCUZ_ROOT.'/'.$filename,'w');
fwrite($fp,$sitemap);
fclose($fp);
?>第二步:
登录discuz后台 - 工具 - 计划任务 - 新增计划任务 - 选择执行周期 - 添加任务脚本(就是新建的文件名)
每次执行后会在网站根目录生成一个sitemap.xml文件
其他类型的文件可根据上述代码自行修改
大功告成!
页: [1]
查看完整版本: Discuz论坛自动网站地图sitemap教程(完整版)