天天看點

php 生成網站地圖sitemap.xml

生成網站地圖,所需代碼如下:

/**
     * 生成sitemap網站地圖
     */
    public function updateSitemap(){
        $str='<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';



        $data=[];
        //擷取資料
        $list=db('articles')->field('id,title,category_id,created_at')->where(['is_timing'=>0])->order('created_at desc')->select();

        //案例展示id集合
        $cate_ids=db('categories')->where('pid',1)->column('id');
        //新聞資訊id集合
        $news_ids=db('categories')->where('pid',4)->column('id');
        $i=0;
        $m=0;
        foreach($list as $val){

            //查詢文章分類
            $cate_info=db('categories')->field('id,pid,url')->where('id',$val['category_id'])->find();
            $url=LOCATION_URL.$cate_info['url'].".html";
            if(in_array($val['category_id'],$cate_ids)){
                $url=LOCATION_URL."/case/".$val['id'].".html";
            }elseif(in_array($val['category_id'],$news_ids)){
                $url=LOCATION_URL."/newsdetail/".$val['id'].".html";
            }elseif($cate_info['url'] == '/fangan/13'){
                $url=LOCATION_URL.'/fangan/'.$val['id'].'.html';
            }
            $str.="<url>";
            $str.='<loc>'.$url.'</loc>';
            $str.='<priority>0.7</priority>';
            $str.='<lastmod>'.$val['created_at'].'</lastmod>';
            $str.='<changefreq>daily</changefreq>';
            $str.="</url>";
        }
        //二級頁
        $child_list=Db::name('categories')->field('id,name,url')->where(['pid'=>['neq',0],'id'=>['not in',[6,11,20,24]]])->select();
        foreach($child_list as $child){
            $child_url=LOCATION_URL.$child['url'].".html";
            $str.="<url>";
            $str.='<loc>'.$child_url.'</loc>';
            $str.='<priority>0.7</priority>';
            $str.='<lastmod>'.'2020-11-17'.'</lastmod>';
            $str.='<changefreq>daily</changefreq>';
            $str.="</url>";
        }
        //菜單欄
        $nav_list=Db::name('categories')->field('id,name,url')->where(['pid'=>0])->select();
        foreach($nav_list as $nav){
            $str.="<url>";
            $str.='<loc>'.LOCATION_URL.$nav['url'].'.html</loc>';
            $str.='<priority>0.8</priority>';
            $str.='<lastmod>'.'2020-11-17'.'</lastmod>';
            $str.='<changefreq>daily</changefreq>';
            $str.="</url>";
        }




        //增加頁數
        $all_cate_ids=array_merge($cate_ids,$news_ids);

        foreach($cate_ids as $v){
            //案例展示總頁數
            $name=Db::name('categories')->where(['id'=>$v])->value('url');
            $num=$this->get_page_num($v,10);
            if($num>1){
                $str.=$this->add_url($num,$name,$v);
            }

        }

        $str.="<url>";
        $str.='<loc>'.LOCATION_URL.'</loc>';
        $str.='<priority>1.0</priority>';
        $str.='<lastmod>'.'2019-07-31'.'</lastmod>';
        $str.='<changefreq>daily</changefreq>';
        $str.="</url>";


        $str.='</urlset>';

        $file=fopen('sitemap.xml','w');
        fwrite($file,$str);
        fclose($file);
        file_put_contents('sitemap.xml',$str);

    }

    /**
     * 擷取某個分類下的資料總共多少頁
     */
    public function get_page_num($category_id,$num){

        $page_info=Db::name('articles')->where('category_id','in',$category_id)->where(['is_timing'=>0])->paginate($num)->toArray();
        return $page_info['last_page'];
    }
    /**
     * 循環增加sitemap
     */
    public function add_url($num,$name,$cate_id){
        $str='';
        for ($a=2; $a<=$num; $a++) {
            $str.="<url>";
            $str.='<loc>'.LOCATION_URL.$name.'/'.$cate_id.'/page/'.$a.'.html'.'</loc>';
            $str.='<priority>0.8</priority>';
            $str.='<lastmod>'.'2020-11-17'.'</lastmod>';
            $str.='<changefreq>daily</changefreq>';
            $str.="</url>";
        }
        return $str;
    }
           

效果如下:

php 生成網站地圖sitemap.xml