WordPress免插件实现XML网站地图

站点建设评论11.2K2字数 2882阅读9分36秒阅读模式

WordPress有很多生成Sitemap的插件,以前都是用柳城的(Baidu Sitemap Generator ),本着强迫症的原则,在网上搜了下纯代码版的。下面记录下本站实现XML网站地图的过程。可以生成首页、文章、单页面、分类和标签的Sitemap

相关文章: WordPress免插件实现HTML网站地图文章源自亦枫博客-https://yflad.cn/535.html

 文章源自亦枫博客-https://yflad.cn/535.html

WordPress免插件实现XML网站地图文章源自亦枫博客-https://yflad.cn/535.html

 文章源自亦枫博客-https://yflad.cn/535.html

1、新建一个sitemap.php至站点根目录下,复制下面代码粘贴:文章源自亦枫博客-https://yflad.cn/535.html

  1. <?php
  2. require('./wp-blog-header.php');
  3. header("Content-type: text/xml");
  4. header('HTTP/1.1 200 OK');
  5. $posts_to_show = 1000;
  6. echo '<?xml version="1.0" encoding="UTF-8"?>';
  7. echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">'
  8. ?>
  9. <!-- generated-on=<?php echo get_lastpostdate('blog'); ?> Diy By 张戈博客(http://zhangge.net)-->
  10.   <url>
  11.       <loc><?php echo get_home_url(); ?></loc>
  12.       <lastmod><?php $ltime = get_lastpostmodified(GMT);$ltime = gmdate('Y-m-d\TH:i:s+00:00', strtotime($ltime)); echo $ltime; ?></lastmod>
  13.       <changefreq>daily</changefreq>
  14.       <priority>1.0</priority>
  15.   </url>
  16. <?php
  17. /* 文章页面 */
  18. $myposts = get_posts( "numberposts=" . $posts_to_show );
  19. foreach$myposts as $post ) { ?>
  20.   <url>
  21.       <loc><?php the_permalink(); ?></loc>
  22.       <lastmod><?php the_time('c') ?></lastmod>
  23.       <changefreq>monthly</changefreq>
  24.       <priority>0.6</priority>
  25.   </url>
  26. <?php } /* 文章循环结束 */ ?>
  27. <?php
  28. /* 单页面 */
  29. $mypages = get_pages();
  30. if(count($mypages) > 0) {
  31.     foreach($mypages as $page) { ?>
  32.     <url>
  33.       <loc><?php echo get_page_link($page->ID); ?></loc>
  34.       <lastmod><?php echo str_replace(" ","T",get_page($page->ID)->post_modified); ?>+00:00</lastmod>
  35.       <changefreq>weekly</changefreq>
  36.       <priority>0.6</priority>
  37.   </url>
  38. <?php }} /* 单页面循环结束 */ ?>
  39. <?php
  40. /* 博客分类 */
  41. $terms = get_terms('category', 'orderby=name&hide_empty=0' );
  42. $count = count($terms);
  43. if($count > 0){
  44. foreach ($terms as $term) { ?>
  45.     <url>
  46.       <loc><?php echo get_term_link($term$term->slug); ?></loc>
  47.       <changefreq>weekly</changefreq>
  48.       <priority>0.8</priority>
  49.   </url>
  50. <?php }} /* 分类循环结束 */?>
  51. <?php
  52.  /* 标签(可选) */
  53. $tags = get_terms("post_tag");
  54. foreach ( $tags as $key => $tag ) {
  55.     $link = get_term_link( intval($tag->term_id), "post_tag" );
  56.          if ( is_wp_error( $link ) )
  57.           return false;
  58.           $tags$key ]->link = $link;
  59. ?>
  60.  <url>
  61.       <loc><?php echo $link ?></loc>
  62.       <changefreq>monthly</changefreq>
  63.       <priority>0.4</priority>
  64.   </url>
  65. <?php  } /* 标签循环结束 */ ?>
  66. </urlset>

 文章源自亦枫博客-https://yflad.cn/535.html

2、设置url转发规则文件文章源自亦枫博客-https://yflad.cn/535.html

主机是Apache,在.htaccess添加以下重写规则:文章源自亦枫博客-https://yflad.cn/535.html

  1. //Code from http://timle.cn/
  2. RewriteEngine On
  3. RewriteBase /
  4. RewriteRule ^sitemap.xml$ xmlmap.php

主机是nginx,在.htaccess添加以下重写规则:文章源自亦枫博客-https://yflad.cn/535.html

  1. //Code from http://timle.cn/
  2. rewrite ^/sitemap.xml$ /xmlmap.php;

或者编辑已存在的Nginx伪静态规则,新增如下规则后重启nginx:文章源自亦枫博客-https://yflad.cn/535.html

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

主机是IIS,在web.config添加以下重写规则:文章源自亦枫博客-https://yflad.cn/535.html

  1. <rule name="Rewrite to xmlmap.php" stopProcessing="true">
  2. <match url="^sitemap.xml" />
  3. <action type="Rewrite" url="xmlmap.php" />
  4. </rule>

PS:还可以在Robot添加如下规则,标注一下自己的sitemap的地址,不过我感觉没有那个必要,也就没添加了,有兴趣的可以试试:文章源自亦枫博客-https://yflad.cn/535.html

  1. //Code from http://timle.cn/
  2. Sitemap: http://www.timle.cn/sitemap.xml

3、本站演示XML版站点地图:yflad.cn/sitemap.xml文章源自亦枫博客-https://yflad.cn/535.html

 文章源自亦枫博客-https://yflad.cn/535.html

至此已全部结束,有问题的可以在下面留言,或者QQ私我!文章源自亦枫博客-https://yflad.cn/535.html

无知,惹笑了!文章源自亦枫博客-https://yflad.cn/535.html 文章源自亦枫博客-https://yflad.cn/535.html

继续阅读
扫扫关注公众号
weinxin
我的微信
扫扫体验小程序
weinxin
我的公众号
亦枫
  • 本文由 发表于 2017年5月27日 05:34:58
评论  0  访客  0
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定