首页 > 运营 > 帮助中心 > 正文

WordPress网站地图sitemap.xml设置方法_代码免插件

2020-01-27 17:23:24
字体:
来源:转载
供稿:网友

前言:站点地图(sitemap.xml)的作用,相信站长们都有所了解,我就不献宝了。而免插件生成 sitemap.xml,网络上也早就有了纯代码生成的方法。

今天让这个代码更加完善,可以同时生成首页、文章、单页面、分类和标签的 sitemap!

1)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 错新网(https://www.Cuoxin.com)-->
  10.   <url>
  11.       <loc><?php echo get_home_url(); ?></loc>
  12.       <lastmod><?php $ltime = get_lastpostmodified(GMT);$ltime = gmdate('Y-m-dTH: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>

将以上代码保存为 sitemap.php,传到网站根目录。

2)伪静态

Nginx

编辑已存在的 Nginx 伪静态规则,新增如下规则后(平滑)重启 nginx 即可:

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

Apache

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

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

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

比如https://www.Cuoxin.com/sitemap.xml

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表