首页 > 教程 > WP教程 > 进阶教程 > SEO优化

WordPress网站地图sitemap.xml设置方法

SEO优化 2025-04-14 21:35:15

摘要:WordPress网站地图sitemap.xml设置方法_代码免插件|wp怎么制作sitemap?WordPress怎么生成sitemap?wp如何生成网站地图sitemap.xml?wp代码实现sitemap功能

前言:站点地图(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.kaituozu.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=namehide_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 看看效果了

比如/sitemap.xml