|
这是一篇介绍dedecms如何定时生成站点地图的文章,定时生成首页也是这个思路,这里用到了dedecms后台的计划任务功能来完成。
下面就正是开始完成此功能:
首先在模板目录放一个sitemap.xml的模板文件。内容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
- <url>
- <loc>/</loc>
- <lastmod>{dede:arclist row=1 titlelen=24 orderby=pubdate}
- [field:pubdate function=strftime('%Y-%m-%d',@me)/]
- {/dede:arclist}</lastmod>
- <changefreq>daily</changefreq>
- <priority>1.0</priority>
- </url>
- {dede:channel row='10' type='top'}
- <url>
- <loc>[field:typelink /]</loc>
- <changefreq>daily</changefreq>
- <priority>0.8</priority>
- </url>
- {/dede:channel}
- {dede:arclist row=2000 orderby=pubdate}
- <url>
- <loc>[field:arcurl/]</loc>
- <lastmod>[field:pubdate function=strftime('%Y-%m-%d',@me)/]</lastmod>
- <changefreq>monthly</changefreq>
- </url>
- {/dede:arclist}
- </urlset>
复制代码
写一个计划任务文件命名为generate_sitemap.php,放在/plus/task目录里,文件内容如下:
- <?php
- //定时生成网站地图
- require_once(dirname(__FILE__).'/../../include/common.inc.php');
- include(DEDEINC."/arc.listview.class.php");
- $lv = new ListView();
- //解析模板到字符串
- $lv->PartView = new PartView($lv->TypeID,false);
- $lv->PartView->SetTypeLink($lv->TypeLink);
- $lv->PartView->SetTemplet(DEDETEMPLATE.'/2012/sitemap.xml');
- $html = $lv->PartView->GetResult();
- file_put_contents('../../sitemap.xml',$html);
- ?>
复制代码
在后台添加计划任务,系统——计划任务管理——增加新任务:
任务名称:定时生成网站地图
运行程序:generate_sitemap.php
执行时间自己设置
其他默认即可。
在模板目录的foot.hml文件或其他公共include文件body结束之前加上代码:
在计划任务规定的时间内浏览一下网页来触发计划任务文件执行。如果上次运行时间的时间正常就证明定时任务执行成功。
OK! 定时生成网站地图(sitemap.xml)功能结束,你可以在百度站长平台里提交这个网站地图文件,百度robot会更喜欢你的网站!
|
|