ThinkPHP5生成网站xml地图方法

2020-10-14   阅读:2515   分类:后端    标签: TP5

什么是网站xml地图?

网站地图对于SEO非常重要,在网站中加入网站地图有利于搜索引擎蜘蛛的抓取和收录。

怎样生网站地图?

我们可以通过SEO管理工具或者网站地图在线生成工具,但每次都通过SEO工具去抓取生成网站地图,这样很麻烦,现在教大家另一种方法,使用以下方法生成xml地图,该方法会在网站更目录生成sitemap.xml地图,生成标准的网站地图格式,然后我们在后台点击下更新即可更新网站地图。

控制器Site.php文件代码:

<?php
namespace app\admin\controller;
use think\Controller;
// 网站地图
class Site extends Base
{
public function sitemapxml(){
      $today = date("Y-m-d",time());
      $article=db('article')->field('id,note_cate')->where(array('hide'=>0))->select();

      $domain="https://www.tpxhm.com";
      $data_array=array();  //文章列表

      foreach($article as $k=>$v){
          $data_array[$k]['loc'] = $domain.'/art/.$v['id'].'.html';
          $data_array[$k]['lastmod'] = $today;
          $data_array[$k]['changefreq'] = 'always';
          $data_array[$k]['priority'] = '0.8';
      }
      $all=$data_array;
      $content="<?xml version='1.0' encoding='UTF-8'?>".chr(13)."\n";
      $content.="<?xml-stylesheet type='text/xsl' href='sitemap.xsl'?>\n";
      $content.="<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9' xmlns:mobile='http://www.baidu.com/schemas/sitemap-mobile/1/'>\n";
      foreach($all as $data){
          $content.=$this->create_item($data);
      }
      $content.='</urlset>';
      //halt($content);
      $fp=fopen('sitemap.xml','w+');
      fwrite($fp,$content);
      fclose($fp);
      if($fp){
        return json(['code'=>200,'msg'=>'更新成功','data'=>'/sitemap.xml']);
      }
      
  }

  public function create_item($data){
      $item="<url>\n";
      $item.="<mobile:mobile type='pc,mobile'/>\n";
      $item.="<loc>".$data['loc']."</loc>\n";
      $item.="<lastmod>".$data['lastmod']."</lastmod>\n";
      $item.="<changefreq>".$data['changefreq']."</changefreq>\n";
      $item.="<priority>".$data['priority']."</priority>\n";
      $item.="</url>\n";
      return $item;
  }
}
?>

sitemap.xsl样式文件

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
                xmlns:html="http://www.w3.org/TR/REC-html40"
                xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
	<xsl:template match="/">
		<html xmlns="http://www.w3.org/1999/xhtml">
			<head>
				<title>XML Sitemap</title>
				<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
				<style type="text/css">
					body {font-family:"Lucida Grande","Lucida Sans Unicode",Tahoma,Verdana;font-size:13px;}
					h1 {color:#0099CC;}
					#intro {background-color:#CFEBF7;border:1px #2580B2 solid;padding:5px 13px 5px 13px;margin:10px;}
					#intro p {line-height:16.8667px;}
					td {font-size:11px;}
					th {text-align:left;padding-right:30px;font-size:11px;}
					tr.high {background-color:whitesmoke;}
					#footer {padding:2px;margin:10px;font-size:8pt;color:gray;}
					#footer a {color:gray;}
					a {color:black;}
				</style>
			</head>
			<body>
				<h1>XML Sitemap</h1>
				<div id="intro">
					<p>
						This is a XML Sitemap which is supposed to be processed by search engines like <a href="http://www.google.com">Google</a>, <a href="http://search.msn.com">MSN Search</a> and <a href="http://www.yahoo.com">YAHOO</a>.<br />
						With such a sitemap, it's much easier for the crawlers to see the complete structure of your site and retrieve it more efficiently.</p>
	        </div>
				<div id="content">
					<table cellpadding="5">
						<tr style="border-bottom:1px black solid;">
							<th>URL</th>
							<th>Priority</th>
							<th>Change Frequency</th>
							<th>Last Change</th>
						</tr>
						<xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/>
						<xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
						<xsl:for-each select="sitemap:urlset/sitemap:url">
							<tr>
								<xsl:if test="position() mod 2 != 1">
									<xsl:attribute  name="class">high</xsl:attribute>
								</xsl:if>
								<td>
									<xsl:variable name="itemURL">
										<xsl:value-of select="sitemap:loc"/>
									</xsl:variable>
									<a href="{$itemURL}">
										<xsl:value-of select="sitemap:loc"/>
									</a>
								</td>
								<td>
									<xsl:value-of select="concat(sitemap:priority*100,'%')"/>
								</td>
								<td>
									<xsl:value-of select="concat(translate(substring(sitemap:changefreq, 1, 1),concat($lower, $upper),concat($upper, $lower)),substring(sitemap:changefreq, 2))"/>
								</td>
								<td>
									<xsl:value-of select="concat(substring(sitemap:lastmod,0,11),concat(' ', substring(sitemap:lastmod,12,5)))"/>
								</td>
							</tr>
						</xsl:for-each>
					</table>
				</div>
			</body>
		</html>
	</xsl:template>
</xsl:stylesheet>

生成的xml格式:

<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet type='text/xsl' href='sitemap.xsl'?>
<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9' xmlns:mobile='http://www.baidu.com/schemas/sitemap-mobile/1/'>
<url>
<mobile:mobile type='pc,mobile'/>
<loc>https://www.tpxhm.com/</loc>
<lastmod>2020-10-12</lastmod>
<changefreq>always</changefreq>
<priority>1</priority>
</url>
<url>
<mobile:mobile type='pc,mobile'/>
<loc>https://www.tpxhm.com/f.html</loc>
<lastmod>2020-10-12</lastmod>
<changefreq>always</changefreq>
<priority>0.9</priority>
</url>
</urlset>

效果预览展示:https://www.tpxhm.com/sitemap.xml

image.png


【腾讯云】 爆款2核2G3M云服务器首年 61元,2核2G4M云服务器新老同享 99元/年,续费同价

‘简忆博客’微信公众号 扫码关注‘简忆博客’微信公众号,获取最新文章动态
转载:请说明文章出处“来源简忆博客”。http://www.tpxhm.com/adetail/467.html

×
觉得文章有用就打赏一下文章作者
微信扫一扫打赏 微信扫一扫打赏
支付宝扫一扫打赏 支付宝扫一扫打赏

文章评论(0)

登录
简忆博客壁纸一
简忆博客壁纸二
简忆博客壁纸三
简忆博客壁纸四
简忆博客壁纸五
简忆博客壁纸六
简忆博客壁纸七
简忆博客壁纸八
头像

简忆博客
勤于学习,乐于分享

置顶推荐

打赏本站

如果你觉得本站很棒,可以通过扫码支付打赏哦!
微信扫码:你说多少就多少~
微信扫码
支付宝扫码:你说多少就多少~
支付宝扫码
×