现在入驻立享优惠!详情咨询客服>> QQ:115904045 QQ群:160457583 VIP群:540574329

您当前的位置:首页>>CSS教程>>PbootCMS教程

【腾讯云】爆款1核2G云服务器首年48元,还有iPad Pro、Bose耳机、京东卡等你来抽! 爆款云产品,新客特惠全年最低价,云服务器低至0.4折起

给PbootCms增加sitemap.html网站地图页面

来源:超级管理员 发布时间:2021-11-18 热度:352 ℃

PbootCms默认的sitemap是xml格式,对网站前端来说显示不够友好。毕竟这玩意是给搜索引擎看的。

我们来给网站增加一个sitemap.html页面。

方法有很多,你可以通过后台新建一个专题页,命名为sitemap来实现。

这个方法有些缺点,就是在标签循环过程中要手动去除该页面。

作为一个强迫症患者,这个不能忍。

今天我们就来动手给PbootCMS增加一个sitemap.html功能页面

话不多说,上教程,以PbootCms v3.1.2示例,其他版本略微差异:

操作步骤

1、打开路由管理页面,路径:apps/common/route.php

大约在40行,注释内容是关于网站地图的前端路由,我们替换成以下内容:

        // 前台及接口路径统一小写URL
        // =======前台路由============
        'home/sitemap.html' => 'home/Sitemap/index', // 站点地图1
        'home/sitemap.xml' => 'home/Sitemap/xml', // 站点地图XML格式
        'home/sitemap.txt' => 'home/Sitemap/linkTxt', // 站点地图TXT格式
        //'home/sitemap' => 'home/Sitemap/xml', // 站点地图默认XML

2、打开sitemap管理控制器,路径:apps/home/controller/SitemapController.php

替换为:

<?php
/**
 * @copyright (C)2016-2099 Hnaoyun Inc.
 * @author XingMeng
 * @email hnxsh@foxmail.com
 * @date 2018年7月15日
 *  生成sitemap文件
 */
namespace apphomecontroller;
use coreasicController;
use apphomemodelSitemapModel;
use coreasicUrl;
class SitemapController extends Controller
{
    protected $parser;
    protected $htmldir;
    protected $model;
    public function __construct()
    {
        $this->model = new SitemapModel();
        $this->parser = new ParserController();
        $this->htmldir = $this->config('tpl_html_dir') ? $this->config('tpl_html_dir') . '/' : '';
    }
    public function index(){
        $tpl='sitemap.html';
        $content = parent::parser($this->htmldir . $tpl); // 框架标签解析
        $content = $this->parser->parserBefore($content); // CMS公共标签前置解析
        $content = str_replace('{pboot:pagetitle}','网站地图-{pboot:sitetitle}-{pboot:sitesubtitle}', $content);
        $content = $this->parser->parserPositionLabel($content, 0, '网站地图', Url::home('sitemap')); // CMS当前位置标签解析
        $content = $this->parser->parserSpecialPageSortLabel($content, - 1, '网站地图', Url::home('sitemap')); // 解析分类标签
        $content = $this->parser->parserSearchLabel($content); // 搜索结果标签
        $content = $this->parser->parserAfter($content); // CMS公共标签后置解析
        echo $content; // 搜索页面不缓存
        exit();
    }
    public function xml()
    {
        header("Content-type:text/xml;charset=utf-8");
        $str = '<?xml version="1.0" encoding="UTF-8"?>' . "
";
        $str .= '<urlset>' . "
";
        $str .= $this->makeNode('', date('Y-m-d'), '1.00', 'always'); // 根目录
        
        $sorts = $this->model->getSorts();
        $Parser = new ParserController();
        foreach ($sorts as $value) {
            if ($value->outlink) {
                continue;
            } elseif ($value->type == 1) {
                $link = $Parser->parserLink(1, $value->urlname, 'about', $value->scode, $value->filename);
                $str .= $this->makeNode($link, date('Y-m-d'), '0.80', 'daily');
            } else {
                $link = $Parser->parserLink(2, $value->urlname, 'list', $value->scode, $value->filename);
                $str .= $this->makeNode($link, date('Y-m-d'), '0.80', 'daily');
                $contents = $this->model->getSortContent($value->scode);
                foreach ($contents as $value2) {
                    if ($value2->outlink) { // 外链
                        continue;
                    } else {
                        $link = $Parser->parserLink(2, $value2->urlname, 'content', $value2->scode, $value2->sortfilename, $value2->id, $value2->filename);
                    }
                    $str .= $this->makeNode($link, date('Y-m-d', strtotime($value2->date)), '0.60', 'daily');
                }
            }
        }
        echo $str . "
</urlset>";
    }
    // 生成结点信息
    private function makeNode($link, $date, $priority = 0.60, $changefreq = 'always')
    {
        $node = '
<url>
    <loc>' . get_http_url() . $link . '</loc>
    <priority>' . $priority . '</priority>
    <lastmod>' . $date . '</lastmod>
    <changefreq>' . $changefreq . '</changefreq>
</url>';
        return $node;
    }
    // 文本格式
    public function linkTxt()
    {
        $sorts = $this->model->getSorts();
        $Parser = new ParserController();
        $str = get_http_url() . "
";
        foreach ($sorts as $value) {
            if ($value->outlink) {
                continue;
            } elseif ($value->type == 1) {
                $link = $Parser->parserLink(1, $value->urlname, 'about', $value->scode, $value->filename);
            } else {
                $link = $Parser->parserLink(2, $value->urlname, 'list', $value->scode, $value->filename);
                $str .= get_http_url() . $link . "
";
                $contents = $this->model->getSortContent($value->scode);
                foreach ($contents as $value2) {
                    if ($value2->outlink) { // 外链
                        continue;
                    } else {
                        $link = $Parser->parserLink(2, $value2->urlname, 'content', $value2->scode, $value2->sortfilename, $value2->id, $value2->filename);
                    }
                    $str .= get_http_url() . $link . "
";
                }
            }
        }
        echo $str;
    }
}

3、在网站模板下新建一个sitemap.html页面,通常是在 template模板文件夹htmlsitemap.html

在该模板中引入公用文件后。在正文部分插入以下代码:

<div class="sitemap">
    {pboot:nav}
    <dl>
        <dt><a href="[nav:link]">[nav:name]</a></dt>
        {pboot:if([nav:soncount]>0)}
        <dd>
        {pboot:2nav parent=[nav:scode]}
        <a href="[2nav:link]">[2nav:name]</a>
        {/pboot:2nav}
        </dd>
        {/pboot:if}
    </dl>
    {/pboot:nav}
</div>

到此,html格式的网站地图制作完成。

效果可以看本站的网站地图:网站地图-尚艺模板网

发表评论:

评论记录:

未查询到任何数据!