wordpress純代碼生成sitemap.xml網(wǎng)站地圖

wordpress默認不帶xml網(wǎng)站地圖,博客之前一直使用的是Google xml插件,今天帖一下純代碼生成xml網(wǎng)站地圖,能少用一款插件就少用一款插件。

創(chuàng)建sitemap.php

網(wǎng)站根目錄下新建一個sitemap.php文件,內(nèi)容如下:

<?phprequire('./wp-blog-header.php');header("Content-type: text/xml");header('HTTP/1.1 200 OK');$posts_to_show=1000;echo'<?xml version="1.0"encoding="UTF-8"?>';echo '<urlsetxmlns="https://www.sitemaps.org/schemas/sitemap/0.9"xmlns:mobile="https://www.baidu.com/schemas/sitemap-mobile/1/">'?><url><loc><?phpechoget_home_url();?></loc><mobile:mobiletype="pc,mobile"/><lastmod><?php$ltime=get_lastpostmodified(GMT);$ltime=gmdate('Y-m-dTH:i:s+00:00',strtotime($ltime));echo$ltime;?></lastmod><changefreq>daily</changefreq><priority>1.0</priority></url><?php/* 文章頁面 */$myposts=get_posts("numberposts=".$posts_to_show);foreach($mypostsas$post){?><url><loc><?phpthe_permalink();?></loc><mobile:mobiletype="pc,mobile"/><lastmod><?phpthe_time('c')?></lastmod><changefreq>monthly</changefreq><priority>0.6</priority></url><?php}/* 文章循環(huán)結(jié)束 */?><?php/* 單頁面 */$mypages=get_pages();if(count($mypages)>0){foreach($mypagesas$page){?><url><loc><?phpechoget_page_link($page->ID);?></loc><mobile:mobiletype="pc,mobile"/><lastmod><?phpechostr_replace(" ","T",get_page($page->ID)->post_modified);?>+00:00</lastmod><changefreq>weekly</changefreq><priority>0.6</priority></url><?php}}/* 單頁面循環(huán)結(jié)束 */?><?php/* 博客分類 */$terms=get_terms('category','orderby=name&hide_empty=0');$count=count($terms);if($count>0){foreach($termsas$term){?><url><loc><?phpechoget_term_link($term,$term->slug);?></loc><mobile:mobiletype="pc,mobile"/><changefreq>weekly</changefreq><priority>0.8</priority></url><?php}}/* 分類循環(huán)結(jié)束 */?><?php/* 標簽(可選) */$tags=get_terms("post_tag");foreach($tagsas$key=>$tag){$link=get_term_link(intval($tag->term_id),"post_tag");if(is_wp_error($link))returnfalse;$tags[$key]->link=$link;?><url><loc><?phpecho$link;?></loc><mobile:mobiletype="pc,mobile"/><changefreq>monthly</changefreq><priority>0.4</priority></url><?php}/* 標簽循環(huán)結(jié)束 */?></urlset>


設(shè)置偽靜態(tài)

nginx添加如下偽靜態(tài)規(guī)則:

rewrite^/sitemap.xml$/sitemap.php last;


訪問:你的域名/sitemap.xml

鉆芒博客xml站點地圖:https://www.zmki.cn/sitemap.xml


文章源自:鉆芒博客?https://www.zmki.cn/3533.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容