Forgot Password
 Register
View: 3476|Reply: 1

Generate sitemap.xml for discuz! forum

[Copy Link]
Posted on 2024-04-08 11:36:56 | Show all floors |Read Mode
  1. <?php
  2. /*
  3. * $ 自动生成网站地图sitemap.xml by discuzstore.com
  4. * 1、discuz后台添加定时任务:后台–工具–计划任务–新增,名字随便,提交
  5. * 2、然后编辑,任务脚本:cron_sitemap.php
  6. */
  7. if(!defined('IN_DISCUZ')) {
  8.         exit('Access Denied');
  9. }
  10. $cfg_updateperi='60';//协议文件更新周期的上限,单位为分钟
  11. $CHARSET='utf-8';// 选择编码方式:utf-8 or gbk
  12. $web_root=$_G['siteurl'];//根网址
  13. /******************************************自动生成网站地图****************************************************/
  14. $txtContent = '';
  15. $sitemap="<?xml version="1.0" encoding="UTF-8"?>\n";
  16. $sitemap.="<urlset\n";
  17. $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";
  18.         //1.文章
  19.         $queryArticle = DB::query("SELECT aid FROM ".DB::table('portal_article_title').' ORDER BY aid DESC');
  20.         while($articleaid = DB::fetch($queryArticle)){
  21.             $link = $web_root."article-{$articleaid['aid']}-1.html";//注意静态规则
  22.                 $txtContent .= $link."\n";
  23.                 $t=time();
  24.                 $riqi=date("Y-m-d",$t);
  25.                 $priority=rand(1,10)/10;
  26. $sitemap.="<url>\n";
  27. $sitemap.="<loc>$link</loc>\n";
  28. $sitemap.="<priority>$priority</priority>\n";
  29. $sitemap.="<lastmod>$riqi</lastmod>\n";
  30. $sitemap.="<changefreq>weekly</changefreq>\n";
  31. $sitemap.="</url>\n";
  32.     }
  33.         //2.帖子
  34.     $queryThread = DB::query("SELECT tid FROM ".DB::table('forum_thread').' WHERE displayorder=0 ORDER BY tid DESC');
  35.     while($threadfid = DB::fetch($queryThread)){
  36.                 $link = $web_root."thread-{$threadfid['tid']}-1-1.html";//注意静态规则
  37.                 $txtContent .= $link."\n";
  38.                 $t=time();
  39.                 $riqi=date("Y-m-d",$t);
  40.                 $priority=rand(1,10)/10;
  41. $sitemap.="<url>\n";
  42. $sitemap.="<loc>$link</loc>\n";
  43. $sitemap.="<priority>$priority</priority>\n";
  44. $sitemap.="<lastmod>$riqi</lastmod>\n";
  45. $sitemap.="<changefreq>weekly</changefreq>\n";
  46. $sitemap.="</url>\n";
  47.     }
  48.     $sitemap .= "</urlset>\n";
  49.         //写入xml文件
  50.     $fp = fopen(DISCUZ_ROOT.'/sitemap.xml','w');
  51.     fwrite($fp,$sitemap);
  52.     fclose($fp);
  53.         //写入txt文件
  54.         $fopen = fopen(DISCUZ_ROOT.'/sitemap.txt',"w+");
  55.     fwrite($fopen,$txtContent);
  56.     fclose($fopen);
  57. ?>
Copy Code
Save these code as: cron_sitemap.php  to path: /source/include/cron/

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

Use Props Report

 Thread Author| Posted on 2024-04-30 22:48:51 | Show all floors
Make sure your cron_sitemap.php file located in /source/include/cron/

Reply

Use Props Report

You need to log in before you can reply Login | Register

Forum Credit Rules

Close

Editors SelectedPrevious /2 Next

Archiver|Mobile|Darkroom|Privacy|DSC

GMT+7, 2024-11-21 14:20 , Processed in 0.013648 second(s), 17 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

Quick Reply Back to Top Return to List