当前位置:首页 > 数字漫游 > 正文内容

discuz x3.5 网站生成sitemap

reik223个月前 (06-05)数字漫游185

使用说明

  1. 将上述代码保存为 cron_sitemap.php 并上传到 Discuz! 的 source/include/cron/ 目录下
  2. 在 Discuz! 后台添加计划任务:
    • 进入 后台 > 工具 > 计划任务 > 新增

    • 填写任务名称(如 "生成网站地图")

    • 选择执行频率(建议每天或每周)

    • 在 "任务脚本" 下拉菜单中选择 cron_sitemap.php

    • 保存设置

  3. 确保网站根目录可写,以便生成 sitemap.xml 和 sitemap.txt 文件
  4. 如果你的网站使用了自定义 URL 规则,请调整代码中的链接格式以匹配实际情况


<?php/*
 * $ 自动生成网站地图sitemap.xml
 * 1、discuz后台添加定时任务:后台–工具–计划任务–新增,名字随便,提交
 * 2、然后编辑,任务脚本:cron_sitemap.php
 */if(!defined('IN_DISCUZ')) {
    exit('Access Denied');}$cfg_updateperi = '60'; // 协议文件更新周期的上限,单位为分钟$CHARSET = 'utf-8'; // 建议使用 utf-8 编码$batch_size = 1000; // 每次处理的记录数/******************************************自动生成网站地图****************************************************/$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";// 获取当前时间$now = time();$current_date = date("Y-m-d", $now);// 1. 文章$article_count = 0;$article_page = 0;do {
    $start = $article_page * $batch_size;
    $queryArticle = DB::query("SELECT aid FROM ".DB::table('portal_article_title')." ORDER BY aid DESC LIMIT $start, $batch_size");
    $article_page++;
    $article_count = 0;
    
    while($article = DB::fetch($queryArticle)) {
        $article_count++;
        $link = dhtmlspecialchars("{$_G['siteurl']}article-{$article['aid']}-1.html"); // 注意静态规则
        $txtContent .= $link."\n";
        
        // 根据文章ID生成优先级,新文章优先级更高
        $priority = min(1.0, max(0.5, 1.0 - ($article_page * $batch_size - $start) / 10000));
        
        $sitemap .= "<url>\n";
        $sitemap .= "<loc>$link</loc>\n";
        $sitemap .= "<priority>".number_format($priority, 1)."</priority>\n";
        $sitemap .= "<lastmod>$current_date</lastmod>\n";
        $sitemap .= "<changefreq>weekly</changefreq>\n";
        $sitemap .= "</url>\n";
    }} while ($article_count >= $batch_size);// 2. 帖子$thread_count = 0;$thread_page = 0;do {
    $start = $thread_page * $batch_size;
    $queryThread = DB::query("SELECT tid FROM ".DB::table('forum_thread')." WHERE displayorder=0 ORDER BY tid DESC LIMIT $start, $batch_size");
    $thread_page++;
    $thread_count = 0;
    
    while($thread = DB::fetch($queryThread)) {
        $thread_count++;
        $link = dhtmlspecialchars("{$_G['siteurl']}thread-{$thread['tid']}-1-1.html"); // 注意静态规则
        $txtContent .= $link."\n";
        
        // 根据帖子ID生成优先级,新帖子优先级更高
        $priority = min(1.0, max(0.5, 1.0 - ($thread_page * $batch_size - $start) / 10000));
        
        $sitemap .= "<url>\n";
        $sitemap .= "<loc>$link</loc>\n";
        $sitemap .= "<priority>".number_format($priority, 1)."</priority>\n";
        $sitemap .= "<lastmod>$current_date</lastmod>\n";
        $sitemap .= "<changefreq>daily</changefreq>\n"; // 帖子更新频率设为 daily
        $sitemap .= "</url>\n";
    }} while ($thread_count >= $batch_size);$sitemap .= "</urlset>\n";// 写入xml文件$xml_file = DISCUZ_ROOT.'/sitemap.xml';$fp = @fopen($xml_file, 'w');if ($fp) {
    if (fwrite($fp, $sitemap) === false) {
        log_error("无法写入内容到 $xml_file");
    }
    fclose($fp);} else {
    log_error("无法打开 $xml_file 进行写入");}// 写入txt文件$txt_file = DISCUZ_ROOT.'/sitemap.txt';$fopen = @fopen($txt_file, "w+");if ($fopen) {
    if (fwrite($fopen, $txtContent) === false) {
        log_error("无法写入内容到 $txt_file");
    }
    fclose($fopen);} else {
    log_error("无法打开 $txt_file 进行写入");}// 简单的错误日志函数function log_error($message) {
    $log_file = DISCUZ_ROOT.'/sitemap_error.log';
    $fp = @fopen($log_file, 'a');
    if ($fp) {
        fwrite($fp, date('Y-m-d H:i:s') . " - $message\n");
        fclose($fp);
    }}?>

声明:本文仅用于技术交流与合法用途,禁止用于任何违反当地法律的行为。

本文由 网络资源分享 发布,如需转载请注明出处。

分享给朋友:

“discuz x3.5 网站生成sitemap” 的相关文章

Telegram 注册及使用全攻略

Telegram(电报)是由俄罗斯杜洛夫兄弟于 2013 年创建的加密即时通讯软件,每月活跃用户高达 4 亿。它旨在保护用户隐私安全,消息传输多层加密,能有效抵御网络运营商和黑客窥探。其功能与常见通讯软件类似,可传输多种文件类型,但也具备独特优势,像开设加密聊天室、消息阅后自焚、发送禁止下载保存的媒...

最新Telegram双向私聊机器人,完全开源,完全免费,支持高度自定义!nfd---感谢酒神

一个基于 Cloudflare Worker 的 Telegram 消息转发机器人,集成 反欺诈 功能。🎯 主要特点✅ 基于 Cloudflare Worker 搭建低成本,仅需一个 JS 文件无需额外域名,使用 Cloudflare Worker 自带域名数据永久存储...

最热门的双币信用卡,免年费

最热门的双币信用卡,免年费

双币卡的用例是非常多的,无论是留学还是海淘,有着诸多的应用场景。我平时也会逛逛各类网站,一般的域名主机商都必须使用信用卡,例如甲骨文和谷歌云!还有就是注册其他地区的AppStore购买游戏应用等!双币信用卡可不仅仅可以支付美元,主流的欧元、日元、加币等都可以直接支付,还款日会按照当日汇率折算成人民币...

WIN10+优化小工具 v1.3.1 - 果核剥壳

WIN10+优化小工具 v1.3.1 - 果核剥壳

一款聚合了系统快捷设置的小工具,一个界面就能完成常用设置的修改,使用之前,先点击读取本机设置避免数据错乱,然后根据需要勾选,完成后点击重启资源管理器即可。软件截图软件说明吾爱论坛 @辛卯易语言写的,可能存在误报毒的可能;每步操作透明显示放心使用;原帖地址:https://www.52pojie.cn...

Hexo还是Hugo?Typecho还是WordPress?

博客搭建从来都不是一件简单事,框架众多,风格各异。亲身体验,从WordPress → Hexo → Hugo → Typecho → Halo,又回到WordPress,为你梳理各大主流博客框架的优缺点,帮助你找到最适合自己的那一个。一、Hexo:入门级静态博客框架关键词:免费部署、主题丰富、极客风...