今天有时间来分享一篇本人结合AI来修改完成的PHPCMS伪静态自动同步网站栏目与内容URL到sitemaps.xml网站地图文件的方法。采用自动更新sitemap内容有助于搜索引擎更好的发现处理网站新内容与网站URL结构,对于网站的优化起到一定的辅助作用。下面开始正文:
首页,我们需要在内容模块中新增加一个生成sitemap的方法,比如我在/phpcms/modules/content/新建了一个sitemap.php的文件控制器,用来映射模板和调用必要参数。直接上代码:
' . PHP_EOL;
$input = pc_base::load_sys_class('input');
$siteid = intval($_GET['siteid']);
if(!$siteid) $siteid = 1;
$time = time();
// URL规则
$urlrule = getcache('urlrules','commons');
$urlrule = str_replace('|', '~',$urlrule['27']);
$ccatid = array(1, 2, 8, 9, 10, 11, 23, 30, 24, 25, 26, 27, 28, 29, 13, 14, 16, 17);
// 初始化数据库模型
$this->sitemap_db = pc_base::load_model('content_model');
$this->sitemap_db->table_name = $this->sitemap_db->db_tablepre . "news";
$sitemap_db = $this->sitemap_db;
// 设置查询条件
// 使用 'IN' 操作符来包含指定的 catid
$where = array(
'status' => 99,
'typeid' => 0,
'catid' => $ccatid
);
$pagesize = 1000;
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// 执行查询
$infos = $sitemap_db->listinfo($where, 'updatetime DESC', $page, $pagesize, '', '9', $urlrule);
$pages = $sitemap_db->pages;
$totalnums = $sitemap_db->number;
pc_base::load_sys_class('service')->assign([
'page' => $page,
'infos' => $infos,
'pages' => $pages,
]);
// 加载并显示模板
include template('content', 'sitemapx');
}
public function sitemapl(){
// 直接输出XML头,避免模板解析问题
header('Content-Type: text/xml; charset=utf-8');
echo '' . PHP_EOL;
$input = pc_base::load_sys_class('input');
$siteid = intval($_GET['siteid']);
if(!$siteid) $siteid = 1;
$time = time();
// URL规则
$urlrule = getcache('urlrules','commons');
$urlrule = str_replace('|', '~',$urlrule['27']);
$ccatid = array(1, 2, 8, 9, 10, 11, 23, 30, 24, 25, 26, 27, 28, 29, 13, 14, 16, 17);
// 初始化数据库模型
$this->sitemap_db = pc_base::load_model('content_model');
$this->sitemap_db->table_name = $this->sitemap_db->db_tablepre . "news";
$sitemap_db = $this->sitemap_db;
// 设置查询条件
// 使用 'IN' 操作符来包含指定的 catid
$where = array(
'status' => 99,
'typeid' => 0,
'catid' => $ccatid
);
$pagesize = 1000;
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// 执行查询
$infos = $sitemap_db->listinfo($where, 'updatetime DESC', $page, $pagesize, '', '9', $urlrule);
$pages = $sitemap_db->pages;
$totalnums = $sitemap_db->number;
$total_pages = ceil($totalnums / $pagesize);
pc_base::load_sys_class('service')->assign([
'page' => $page,
'infos' => $infos,
'pages' => $pages,
]);
// 加载并显示模板
include template('content', 'sitemapl');
}
public function sitemap_tag(){
// 直接输出XML头,避免模板解析问题
header('Content-Type: text/xml; charset=utf-8');
echo '' . PHP_EOL;
$time = time();
$keyword_db = pc_base::load_model('keyword_model');
$input = pc_base::load_sys_class('input');
$siteid = intval($input->get('siteid'));
if(!$siteid) $siteid = 1;
$page = max($input->get('page'), 1);
$pagesize = 5000;
$where = array('siteid' => $siteid); // 修正为数组格式
// 确认listinfo函数的参数列表,这里假设只需要$where, 排序, 分页信息
$infos = $keyword_db->listinfo($where, 'videonum DESC', $page, $pagesize);
$pages = $keyword_db->pages;
// 加载并显示模板
include template('content', 'sitemap_tag');
}这其中我添加不同的几个方法,第一个是用来输出网站地图导航的,如果内容非常多,需要设置地图分页,我是设置1000个URL分页。其中我又设置了指定栏目的内容放到网站地图中,后面的是把TAG链接也生成了网站地图。
添加好控制器后,我们需要在模板中添加相应的模板文件,在模板中调用出网站地图中需要的数据,比如首页链接,栏目链接,内容页链接,TAG链接等。至于模板文件的写法,我也一并贴出代码提供参考:
{if $page <2} {pc:content action="category" catid="0" num="30" siteid="$siteid" order="listorder ASC" cache="360"} {loop $data $r} {APP_PATH} {date('Y-m-d', $time)} daily 1.0 {if $r['child']} {pc:content action="category" catid="$r['catid']" num="15" siteid="$siteid" order="listorder ASC" cache="360"} {loop $data $r} {$r['url']} {date('Y-m-d', $time)} daily 0.9 {/loop} {/pc} {/if} {/loop} {/pc} {/if} {loop $infos $r} {$r['url']} {date('Y-m-d', $time)} daily 0.9 {/loop} {$r[url]} {date('Y-m-d', $r[updatetime])} daily 0.8
另外一个模板代码:这是网站地图导航自动生成所有内容的分页网站地图
{APP_PATH} {date('Y-m-d', $time)} daily 1.0 {APP_PATH}sitemap_p{$n}.xml {date('Y-m-d', $time)} daily 1.0
这是TAG的方法:
{loop $infos $r} {APP_PATH} {date('Y-m-d', $time)} daily 1.0 {/loop} {APP_PATH}tag-{$r['keyword']}-1.html {date('Y-m-d', $time)} daily 0.8
做好这些后,我们需要在网站伪静态规则添加规则,并在配置文件中设置好,才能完美运行。

然后是伪静态配置的修改增加:
"sitemap_p([0-9]+).xml" => "index.php?m=content&c=sitemapx&page=$1", "sitemapl.xml" => "index.php?m=content&c=sitemapx&a=sitemapl", "sitemap_tag.xml" => "index.php?m=content&c=sitemapx&a=sitemap_tag",
到这里基本就完成了。有什么不明白的可以联系优化哥帮忙解决。
