phpcms v9网站如何生成地图sitemap静态页面
第一步:
后台添加一个菜单,如下填写:
第二步,将以下代码保存为 sitemap.php 文件放到 phpcmsmodulesadminsitemap.php :
<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('ad终认灯谁光许采min','admin',0);
pc_base::load_sys_class('form', '', 0);
class sitemap extends admin {
function __construct() {
parent::__construct();
//栏目级别选项
$this->siteid = $this->get_siteid();
$this->categor广ys = 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 = getcach下跳该异减投胜e('sitelist妈省想段义钟','commons');
//根据当前站点,取得文件存放路径
$systemc管值相车称怀汽两古父粉onfig = pc接心机殖发加验优考套_base::load_config('system');
$h算树早临才形tml_root = substr($systemconfig['html_root'], 1);
//判朝的源段理了节指张敌英断当前站点目录,是PH走本期怀PCMS则把文件写到根目录下, 不是则写到分站目录下.(分站目录用由静态文件路经html_root和分站目录dirname组成)
if($siteid==1){
$dir = PHPCMS_PATH;
}else {
$dir 你理罗问训家起= PHPCMS_PATH.$html_***.DIRECTORY_SEPARATOR.$sitecache[$siteid]['dirname'].DIRECTORY_SEPARATOR;
依}
//模型缓存
$modelcache = getcach反双外置穿稳坏控电e('model','commons');
if(!defined('HTML')) def友营棉百督务输ine('HTML',1);
//获取当前站点域名,下面URL时会用到.
$thi北生掉就乐台朝烈别皮盐s_domain = substr($sitecache[$siteid]['domain'], 0,str耐好len($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 文件,放到 phpcmstemplatesdefaultcontentsitemap.html :
{template 'content','header'}
<div id="main_full">
<div id="position"><a href="">首页</a>网站地图</div>
<div id="sitemap">
{loop $CATEGORYS $childid $c}
{php $arrcats = explode(',',$c[arrparentid]);}
{if in_array($catid,$arrcats)}<!-- 判断只有子栏目的 -->
{if $c['ismenu']==1}<!-- 判断栏目是否设置显示 -->
<a href="{$c[url]}">{$c[catname]}</a>
{/if}
{/if}
{/loop}
</p>
{/if}
<!-- 判断没有子栏目且设置显示的 -->
{if $cat['child']==0 and $cat['parentid']==0 and $cat['ismenu']==1}
<h3>{$child}<a href="{$cat[url]}">{$cat[catname]}</a>{$parentid}</h3>{/if}
{/loop} </div>
</div>
{template 'content','footer'}
这里只给你提供一个思路,不涉及具体编码...
简单来说,我们可以使用PHP的filesystem函数来实现这个功能:
首先需要准备2个文件:page.html && add.php
page.html是一个模版文件,我们可以通过它来实现html文件的样式
add.php 可以说是一个控制器,通过它来生成HTML文件
接下来的事很普通,不断的链接数据库,循环遍历你的数据内容
然后设置变量读取你的数据
最后就是通过FS函数来将你的数据写入HTML文件中了:
$path = $row['id'].'.html'; //将你数据的ID值作为HTML的索引页
$fp = fopen('page.html',r);//以只读方式打开模版
$str = fread($fp,filesize("page.html"));//将模版页面内容读入字符串$str中
$str = str_replace("{title}",$title,$str);
$str = str_replace("{content}",$content,$str);
fclose($fp);//替换模版中需要替换的内容,完成后关闭即可
$handle = fopen($path,"w");//打开将要生成的文件
fwrite($handle,$str);//将替换后的模版内容写入新页面中,即生成新的HTML页面