新闻资讯
NEWS
官网建设、网站SEO、手机站优化、短视频制作、专业AI代写等相关资讯

    如何增加一个sitemap.html网站地图功能页面

    日期:2025-04-30 浏览量:40

    PbootCms默认的sitemap是xml格式,在网站前端显示不好看,这是给搜索引擎爬取的。现在来给网站增加一个sitemap.html功能的页面。废话不多讲,上教程。

    操作步骤:


    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模板文件夹


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

    <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格式的网站地图就做好了。    

本文链接:如何增加一个sitemap.html网站地图功能页面 - 生辉AI新媒体 https://caidansheng.cn/97.html