A-A+
phpcms V9 Sitemap静态地图的制作
PHPCMS V9 后台的设置模块里面有 sitemap 的生成工具,在需要的时候直接生成 sitemap 站点地图就可以了,生成的是 xml 文件,生成站点地图一方面可以让搜索引擎更好的去抓取,另一方面也可以让客户了解网站的布局架构,今天夏日博客为大家介绍一种新增 php 函数扩张来为 phpcms v9 添加一个 sitemap 的静态地图,新增扩张函数其实就是二次开发添加的函数了,原理是通过 php 函数来调用网站的所有栏目,然后再生成前端 html 静态页面,这个跟后台生成的 xml 有点区别,这是纯静态的 html,用哪种方法看各自的需求了。
一、新增函数文件sitemap.php,并上传到网站程序目录: phpcms/modules/admin/文件夹下,文件的代码如下:
<?php defined('IN_PHPCMS') or exit('No permission resources.'); pc_base::load_app_class('admin','admin',0); pc_base::load_sys_class('form', '', 0); class sitemap extends admin { function __construct() { parent::__construct(); //栏目级别选项 $this->siteid = $this->get_siteid(); $this->categorys = getcache('category_content_'.$this->siteid,'commons'); } /** * * Enter google sitemap, 百度新闻协议 */ function init() { $hits_db = pc_base::load_model('hits_model'); $CATEGORYS = $this->categorys; //读站点缓存 $siteid = $this->siteid; $sitecache = getcache('sitelist','commons'); //根据当前站点,取得文件存放路径 $systemconfig = pc_base::load_config('system'); $html_root = substr($systemconfig['html_root'], 1); //判断当前站点目录,是PHPCMS则把文件写到根目录下, 不是则写到分站目录下.(分站目录用由静态文件路经html_root和分站目录dirname组成) if($siteid==1){ $dir = PHPCMS_PATH; }else { $dir = PHPCMS_PATH.$html_root.DIRECTORY_SEPARATOR.$sitecache[$siteid]['dirname'].DIRECTORY_SEPARATOR; } //模型缓存 $modelcache = getcache('model','commons'); if(!defined('HTML')) define('HTML',1); //获取当前站点域名,下面URL时会用到. $this_domain = substr($sitecache[$siteid]['domain'], 0,strlen($sitecache[$siteid]['domain'])-1); ob_start(); $file = $dir.'sitemap.html'; include template('content', 'sitemap'); $data = ob_get_contents(); ob_clean(); if(!is_dir($dir)) { mkdir($dir, 0777,1); } file_put_contents($file, $data); @chmod($file,0777); showmessage('当前站点网站地址成功!'); } } ?>
二、相应的需要新增一个模板文件 sitemap.html,并上传到模板所在目录下 phpcms/templates/xiariboke/content/ 下面即可,这里是夏日博客的模板文件目录,具体的模板可以根据自己的站点来选择,默认模板文件夹目录为:phpcms/templates/default/content/,sitemap.html 文件代码如下:
{template 'content','header'} "main_full"> "position">"">首页网站地图 "sitemap"> {loop $CATEGORYS $childid $c} {php $arrcats = explode(',',$c[arrparentid]);} {if in_array($catid,$arrcats)} {if $c['ismenu']==1} "{$c[url]}">{$c[catname]} {/if} {/if} {/loop} {/if} {if $cat['child']==0 and $cat['parentid']==0 and $cat['ismenu']==1} {$child}"{$cat[url]}">{$cat[catname]}{$parentid} {/if} {/loop} {template 'content','footer'}
三、这样程序与模板都已经做好了,最后还需要在后台添加一个静态地图的菜单,如下图所示:
具体位置:扩张》自定义菜单》添加,这样我们再来看一下根目录下是否已经生成了网站地图 sitemap 静态地图,可以通过 ftp 软件查看根目录,或者直接通过浏览器,比如 www.xiariboke/sitemap.html,看是否已经可以正常显示了。