免插件生成站点地图:sitemap.xml

站点地图(sitemap.xml)的作用,相信站长们都有所了解,一般情况下,我们都是用插件生成的,无意间看到大佬的博客有免插件生成站点地图的方法,试了试,成功了,顺便分享给大家。更多查看:风尘博客

该sitemap.xml 包含首页、文章、单页面、分类和标签全部页面,具体实现方法:

一、新建sitemap.php文件,传到网站根目录

 
  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://www.dustyblog.cn/)-->
  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>

完成后,可查看是否成功,例如本站:http://www.dustyblog.cn/sitemap.php

二、静态化:

1、Nginx:新增Nginx 伪静态规则:

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

例如:本站是阿里ECS服务器,在‘/alidata/server/nginx-1.4.4/conf/rewrite’路径下phpwind.conf 文件新增上面规则,重启Nginx即可;实例 

Nginx 重启命令:service nginx restart

2、Apache:编辑网站根目录的 .htaccess ,加入如下规则:

  1. RewriteRule ^(sitemap)\.xml$ $1.php  

做好伪静态规则后,就可以直接访问 sitemap.xml 看看效果 。

猜你喜欢

转载自blog.csdn.net/qq_41690817/article/details/80420035