wordpress纯代码生成sitemap.xml网站地图
生活随笔
收集整理的這篇文章主要介紹了
wordpress纯代码生成sitemap.xml网站地图
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
wordpress默認(rèn)不帶xml網(wǎng)站地圖,風(fēng)君子博客之前一直使用的是Google xml插件,今天帖一下純代碼生成xml網(wǎng)站地圖,能少用一款插件就少用一款插件。
創(chuàng)建sitemap.php
網(wǎng)站根目錄下新建一個(gè)sitemap.php文件,內(nèi)容如下:
<?php
require('./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 '<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="https://www.baidu.com/schemas/sitemap-mobile/1/">'
?>
<url>
<loc><?php echo get_home_url(); ?></loc>
<mobile:mobile type="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( $myposts as $post ) { ?>
<url>
<loc><?php the_permalink(); ?></loc>
<mobile:mobile type="pc,mobile"/>
<lastmod><?php the_time('c') ?></lastmod>
<changefreq>monthly</changefreq>
<priority>0.6</priority>
</url>
<?php } /* 文章循環(huán)結(jié)束 */ ?>
<?php
/* 單頁面 */
$mypages = get_pages();
if(count($mypages) > 0) {
foreach($mypages as $page) { ?>
<url>
<loc><?php echo get_page_link($page->ID); ?></loc>
<mobile:mobile type="pc,mobile"/>
<lastmod><?php echo str_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 ($terms as $term) { ?>
<url>
<loc><?php echo get_term_link($term, $term->slug); ?></loc>
<mobile:mobile type="pc,mobile"/>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>
<?php }} /* 分類循環(huán)結(jié)束 */?>
<?php
/* 標(biāo)簽(可選) */
$tags = get_terms("post_tag");
foreach ( $tags as $key => $tag ) {
$link = get_term_link( intval($tag->term_id), "post_tag" );
if ( is_wp_error( $link ) )
return false;
$tags[ $key ]->link = $link;
?>
<url>
<loc><?php echo $link; ?></loc>
<mobile:mobile type="pc,mobile"/>
<changefreq>monthly</changefreq>
<priority>0.4</priority>
</url>
<?php } /* 標(biāo)簽循環(huán)結(jié)束 */ ?>
</urlset>
設(shè)置偽靜態(tài)
nginx添加如下偽靜態(tài)規(guī)則:
rewrite ^/sitemap.xml$ /sitemap.php last;
訪問:你的域名/sitemap.xml
總結(jié)
以上是生活随笔為你收集整理的wordpress纯代码生成sitemap.xml网站地图的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 文叔叔不限速免费网盘 - 更具特色的免注
- 下一篇: 滴滴回应深圳司机猝死:将为家属提供力所能