前言:
苹果CMS V10版默认的网站地图生成代码过于简陋,且效果低下,也不符合Google的网站地图列表标准,故,重新编排一组代码如下:
一,修改代码
代码修改路径:
/application/route.php
在该文件末尾添加如下代码:
'extrasitemap' => [
0 => '\\app\\extra\\sitemap\\controller\\Index@index',
1 => [],
2 => [],
],
且记,要添加到最后一个);之前。
二,添加代码
在这个位置/application/extra添加2个目录,加好的路径如下:
/application/extra/sitemap
/application/extra/sitemap/controller
在/application/extra/sitemap目录下添加Sitemap.php
代码如下:
<?php
namespace app\extra\sitemap;
use app\common\model\Vod;
use app\common\model\Art;
class Sitemap
{
public function generate()
{
$urls = [];
$vods = Vod::where(['vod_status'=>1])->order('vod_id desc')->limit(10000)->select();
foreach ($vods as $vod) {
$loc = url('vod/detail', ['id'=>$vod['vod_id']], true, true);
$loc = str_replace('/index.php', '', $loc);
$urls[] = [
'loc' => $loc,
'lastmod' => date('Y-m-d\TH:i+08:00', $vod['vod_time']),
'changefreq' => 'weekly',
'priority' => '0.8'
];
}
$arts = Art::where(['art_status'=>1])->order('art_id desc')->limit(10000)->select();
foreach ($arts as $art) {
$loc = url('art/detail', ['id'=>$art['art_id']], true, true);
$loc = str_replace('/index.php', '', $loc);
$urls[] = [
'loc' => $loc,
'lastmod' => date('Y-m-d\TH:i+08:00', $art['art_time']),
'changefreq' => 'weekly',
'priority' => '0.6'
];
}
$xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ';
$xml .= 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ';
$xml .= 'xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 ';
$xml .= 'http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' . "\n";
foreach ($urls as $url) {
$xml .= " <url>\n";
$xml .= " <loc>{$url['loc']}</loc>\n";
$xml .= " <lastmod>{$url['lastmod']}</lastmod>\n";
$xml .= " <changefreq>{$url['changefreq']}</changefreq>\n";
$xml .= " <priority>{$url['priority']}</priority>\n";
$xml .= " </url>\n";
}
$xml .= '</urlset>';
$result = @file_put_contents('./sitemap.xml', $xml);
if ($result === false) {
return 'sitemap.xml生成失败,请检查网站根目录写入权限!';
}
return 'sitemap.xml生成成功!';
}
}
在/application/extra/sitemap/controller目录下添加Index.php
代码如下:
<?php
namespace app\extra\sitemap\controller;
use app\extra\sitemap\Sitemap;
class Index
{
public function index()
{
$sitemap = new Sitemap();
$msg = $sitemap->generate();
echo $msg;
}
}
三,在网站后台添加按钮
在网站后台,首页–>自定义菜单配置–>网站地图生成,/index.php/extrasitemap(另起一行,添加这条内容)–>点击页面底部的“保存”。
再点击网站右上侧(旧版后台)的“操作”–>“清理缓存”。
再去点击网站后台首页左侧边菜单栏上面的“网站地图生成”,就会生成网站地图sitemap.xml文件到网站的根目录了。
如果想让它完全自动去生成网站地图,就把链接添加到自动脚本当中,脚本链接地址:http://你的域名/index.php/extrasitemap 或是 https://你的域名/index.php/extrasitemap
是不是很简单?!
评论抢沙发