商意经

 找回密码
 立即注册
查看: 107|回复: 0

轻松搞定Discuz!建站的sitemap.xml网站地图问题(一)

[复制链接]

1544

主题

3862

帖子

6960

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
6960
发表于 2023-1-18 15:39:36 | 显示全部楼层 |阅读模式
前段时间用DZ+三丰云的免费云服务器搭建了一个宠物网,服务器本身比较稳定,但是发现了一个关于DZ的问题;DZ也算是老牌并且稳定的建站系统了,但是它本身并不具备生成sitemap的功能,对于百度等搜索引擎的收录有狠大的影响,DZ的应用商城也有不少生成sitemap.xml网站地图的插件,但是售价几乎都是1、200元,这么简单的一个功能也成了割韭菜的渠道,今天给大家分享两种借助DZ后台计划任务就能轻松实现sitemap.xml网站地图的生成方法。
方法一(只针对论坛版块生成sitemap.xml):
●建立一个名为“cron_sitemap.php”的文件;
●复制以下代码区的内容到“cron_sitemap.php”中,并保存文件;
PS:注意编码,选择自己DZ对应的编码,我自己用的是UTF-8版本DZ
<?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: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,10000");
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=rand(1,10)/10;
//date("D F d Y",$t);
$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";
$fp = fopen(DISCUZ_ROOT.'/'.$filename,'w');
fwrite($fp,$sitemap);
fclose($fp);
?>●上传“cron_sitemap.php”到source\include\cron目录;
●到discuz后台添加定时任务:后台→工具→计划任务→新增→提交;
●接下来编辑任务,任务脚本填“cron_sitemap.php”



dx3.4、discuz建站系统sitemap生成-新增计划任务



dx3.4、discuz建站系统sitemap生成-编辑计划任务

●首次先手动执行一次该计划任务,在站点根目录会生成名为"sitemap.xml"的文件,这样就完成了网站地图的生成,一劳永逸,就这么简单,不用花一分钱。
本篇只是针对论坛部分的sitemap.xml网站地图生成,下一篇为大家介绍如何生成全站(包括门户文章部分)的sitemap.xml网站地图生成。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|商意经

GMT+8, 2025-4-8 07:13 , Processed in 0.221613 second(s), 23 queries .

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.. 技术支持 by 巅峰设计

快速回复 返回顶部 返回列表