sitemap对seo帮助很大,PHP怎样生成sitemap呢?
用于生成过程显示;可省略只是为了方便后台调用和更美观。
<style>
.table>tbody>tr>td{vertical-align: inherit;}
</style>
<section class="content">
<!--列表-->
<div class="box" style="padding:10px">
<form id="formL" name="formL" action="?pot=batch&n=0" method="post">
<div class="box-body" style="padding:10px 25px;">
<table id="Table_list" class="table table-hover" style="width:100%; padding:0; margin:0; border-top:2px #eee solid">
<tr>
<td colspan="4">
<div id="fabu" style=" width:100%; height:400px; overflow-y:auto;">
<div></div>
</div>
</td>
</tr>
<tr>
<td colspan="4"><div style="padding:10px"><button type="button" onClick="release();" class="btn btn-block btn-success btn-lg"><i class="glyphicon glyphicon-ok" style="color:#fff;"></i> 生成siteMap</button></div></td>
</tr>
<tr>
<td colspan="4" style=" color:#ce0000; text-align:center;">发布期间 请勿刷新或者关闭页面</td>
</tr>
</table>
</div>
</form>
</div>
</section>
</div>
<?php require_once 'require/foot.php';?>
<script>
var su = 0;
function release_html(url){
jQuery.ajax({
type: "post",
url: url,
cache: false,
success: function (data) {
su++;
$("#fabu div").append(data);
//su==调用的文件总数
if(su==1){
$("#fabu div").append("<b style='color:#ce0000;'>发布成功</b><br/>");
}
//自动滚动
$("#fabu").scrollTop($("#fabu div").height());
}
});
}
//整站发布
function release(){
su = 0;
$("#fabu div").text('');
release_html('sitemap.php');
//可以继续调用其他文件
}
</script>
用于生成sitemap;php生成sitemap的核心代码。这个文件中的echo的信息可以美观的在release_html.php文件中显示出来。
require_once $_SERVER['DOCUMENT_ROOT'] . '/php/coon.php';
//服务
$url = 'https://你的域名/';
$cs = '.html';
$txt3 = $url.'service';
$url1 = array();
//修改成你的数据库没有分类的生成
$rs = $pdo->query("select * from web_txt where nav = 7 or nav = 9 or nav = 11 or nav = 12 ");
$t1 = 0;
while ($row = $rs->fetch()) {
$url1[$t1] = $txt3.$row['id'].$cs;
$t1++;
}
//第二个表有分类的列表页生成
$txt9 = $url.'page';
$rs4 = $pdo->query("select * from web_news_left order by orders desc,id desc");
$n=10;
while ($row4 = $rs4->fetch()) {
$url1[$t1] = $txt9.$n.$cs;
if($n == 10){
$url1[$t1] = $txt9.'4'.$cs;
}
$t1++;
$n++;
}
//第三个表有分类的详细页生成
$txt10 = $url.'news';
$rs5 = $pdo->query("select * from web_news order by id desc");
while ($row5 = $rs5->fetch()) {
$url1[$t1] = $txt10.$row5['id'].$cs;
$t1++;
}
//其他单页面生成
$url1[$t1] = $url.'index.html';
$t1++;
$url1[$t1] = $url.'page1.html';
$t1++;
$url1[$t1] = $url.'page6.html';
$t1++;
$url1[$t1] = $url.'page5.html';
$t1++;
$url1[$t1] = $url.'page3.html';
//将所有数据生成sitemap
$content='<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">';
$num = count($url1);
for($i=0; $i<$num; $i++){
$content.="<url> ";
$content.="<loc>".$url1[$i]."</loc> ";
//电脑站的标识 手机站为$content.='<mobile:mobile type="mobile"/>';
$content.='<mobile:mobile type="pc"/>';
$content.="</url> ";
}
$content.='</urlset>';
$fp=fopen('../sitemap.xml','w+');
fwrite($fp,$content);
fclose($fp);
echo '生成PC Sitemap.xml 成功<br/>';
好了,这就是php生成sitemap的全部源码了,这个代码可以和生成静态页的源码进行整合,我会在后续的文章中讲述,还有百度主动推送sitemap。