admin Posted on 2024-04-08 11:36:56

Generate sitemap.xml for discuz! forum

<?php
/*
* $ 自动生成网站地图sitemap.xml by discuzstore.com
* 1、discuz后台添加定时任务:后台–工具–计划任务–新增,名字随便,提交
* 2、然后编辑,任务脚本:cron_sitemap.php
*/
if(!defined('IN_DISCUZ')) {
        exit('Access Denied');
}
$cfg_updateperi='60';//协议文件更新周期的上限,单位为分钟
$CHARSET='utf-8';// 选择编码方式:utf-8 or gbk
$web_root=$_G['siteurl'];//根网址
/******************************************自动生成网站地图****************************************************/
$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 = $web_root."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 = $web_root."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);
?> Save these code as: cron_sitemap.phpto path: /source/include/cron/

Go to Admincp --> Tools --> Scheduled Tasks --> Create new task: cron_sitemap.
Done.

admin Posted on 2024-04-30 22:48:51

Make sure your cron_sitemap.php file located in /source/include/cron/

https://i.imgur.com/UWzkjXH.png
Page: [1]
View Full Version: Generate sitemap.xml for discuz! forum