一键生成sitemap
2018-07-11 11:01:05 来源:admin 点击:1271
首先这个是在后台完成的,所有后台有一个页面用来存放参数
具体布局我就不写了,起码得知道权重有0.1-1,更新频率为always、hourly、daily、weekly、monthly、monthly、yearly、never;这些具体填什么可以查看本站seo模块进行了解。 下面这个样子就行,简单的select选择
当提交的时候,后台进行数据接收。
//主页 $homepage_priority = I("homepage_priority", '1'); $homepage_changefreq = I("homepage_changefreq", 'daily'); //分类页 $category_priority = I("category_priority", '1'); $category_changefreq = I("category_changefreq", 'daily'); //内容页 $content_priority = I("content_priority", '1'); $content_changefreq = I("content_changefreq", 'daily'); if($_POST){ //你的网站 $domain="http://www.zfx.fun/"; //更新时间 $today = date('Y-m-d'); //new一个类库 $sm = new Sitemap(); $smi =$sm->google_sitemap_item($domain, $today, $homepage_changefreq,$homepage_priority); $sm->add_item($smi); /*文章分类页*/ $smi =$sm->google_sitemap_item($domain.'路径.html', $today, $category_changefreq,$category_priority); $sm->add_item($smi); $web_cate = M('数据库表名')->where(array('status'=>1))->order('ordid desc')->select(); foreach($web_cate as &$rs){ $smi =$sm->google_sitemap_item($domain.'web/'.$rs['alias'].'.html', $today, $category_changefreq, $category_priority); $sm->add_item($smi); } /*文章内容页*/ $web = M('数据库表名')->where(array('status'=>1))->order('ordid desc')->select(); foreach($web as &$rs){ $smi =$sm->google_sitemap_item($domain.'web/'.$rs['id'].'.html', $today, $content_changefreq, $content_priority); $sm->add_item($smi); } $sm_file = './sitemaps.xml'; if ($sm->build($sm_file)) { $this->success('sitemap更新成功'); } else { $this->error('sitemap更新失败'); } }else{ $this->display(); }
引入类库
类库如下: /** * 生成xml */namespace Org\Yike;class Sitemap{ var $header = "\n\t"; var $charset = "UTF-8"; var $footer = "\t\n"; var $items = array(); /** * 增加一个新的子项 *@access public *@param google_sitemap item $new_item */ function add_item($new_item) { $this->items[] = $new_item; } /** * 生成XML文档 *@access public *@param string $file_name 如果提供了文件名则生成文件,否则返回字符串. *@return [void|string] */ function build( $file_name = null ) { $map = $this->header . "\n"; foreach ($this->items AS $item) { $loc_zfx=$item['loc']; $lastmod_zfx=$item['lastmod']; $changefreq_zfx=$item['changefreq']; $priority_zfx=$item['priority']; $loc_zfx = htmlentities($loc_zfx, 3); $map .= "\t\t\n\t\t\t$loc_zfx\n"; if ( !empty( $lastmod_zfx ) ) $map .= "\t\t\t$lastmod_zfx\n"; if ( !empty( $changefreq_zfx ) ) $map .= "\t\t\t$changefreq_zfx\n"; if ( !empty( $priority_zfx ) ) $map .= "\t\t\t$priority_zfx\n"; $map .= "\t\t\n"; } $map .= $this->footer . "\n"; if (!is_null($file_name)) { return file_put_contents($file_name, $map); } else { return $map; } } function file_put_contents($file, $data, $flags = '') { $contents = (is_array($data)) ? implode('', $data) : $data; if ($flags == 'FILE_APPEND') { $mode = 'ab+'; } else { $mode = 'wb'; } if (($fp = @fopen($file, $mode)) === false) { return false; } else { $bytes = fwrite($fp, $contents); fclose($fp); return $bytes; } } function google_sitemap_item($loc, $lastmod = '', $changefreq = '', $priority = '') { /*$this->loc = $loc; $this->lastmod = $lastmod; $this->changefreq = $changefreq; $this->priority = $priority;*/ $sm=array(); $sm['loc'] = $loc; $sm['lastmod'] = $lastmod; $sm['changefreq'] = $changefreq; $sm['priority'] = $priority; return $sm; }} //应该好了,之后再服务器首页找sitemap.xml吧。