ThinkPHP5生成网站xml地图方法

1 什么是网站xml地图?

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


2 怎样生网站地图?

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


3 控制器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;
}
}
?>


4 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>


5 生成的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>


6 效果预览展示


相关推荐

  • 获取指定目录下的所有图片信息

    1 获取指定目录下的所有图片信息// 获取指定目录下的所有图片信息 public function getImagesInfo($directory) { $images = []; // 创建递归目录迭代器 $iterator = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY ); // 遍历目录中的每个文件 foreach (

  • Thinkphp各版本的PHP要求

    ThinkPHP 8.0:运行环境要求PHP8.0+,兼容PHP8.3ThinkPHP 6.1:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 6.0:运行环境要求PHP7.2+,兼容PHP8.1ThinkPHP 5.1:运行环境要求PHP5.6+,兼容PHP8.0ThinkPHP 5.0:运行环境要求PHP5.4+,兼容PHP7.3

  • Thinkphp5.1路径常量

    1 配置文件位置根目录/config/template.php2 配置文件内容&lt;?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// +----------------------------------------------------------------------// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reser

  • Thinkphp5.0路径常量

    1 配置文件位置根目录/application/模块名/config.php2 配置文件内容&lt;?php//配置文件return [ // 后台视图输出字符串内容替换 'view_replace_str' =&gt; [ '__PUBLIC__' =&gt; '/', '__STATIC__' =&gt; '/static', '__CONSOLE__' =&gt; '/static/console', '__CONSOLE_CSS__' =&gt; '/static/console/css', '__CONSOLE_IMAGES__' =&gt; '/static/console/ima

  • wp站点防止别人进行DDOS攻击

    1 简介wp站点防止别人进行DDOS攻击。2 配置位置位置:根目录/wp-config.php3 配置内容在【根目录/wp-config.php】文件的开头添加如下代码:if(strpos($_SERVER['REQUEST_URI'], 'xmlrpc.php') !== false){ $protocol = $_SERVER['SERVER_PROTOCOL'] ?? ''; if(!in_array($protocol, ['HTTP/1.1', 'HTTP/2', 'HTTP/2.0', 'HTTP/3'], true)){ $protocol = 'HTTP/1.0'; } hea

  • 只读属性

    1 只读属性简介只读属性的声明方式类似于普通属性,但需要使用 readonly 关键字。2 只读属性例子class Point { public readonly float $x; public readonly float $y; public function __construct(float $x, float $y) { $this-&gt;x = $x; $this-&gt;y = $y; }}$point = new Point(3.5, 2.8);echo $point-&gt;x; // 输出: 3.5echo $point-&gt;y; // 输出: 2.8// 下面的尝