第一步:在 WordPress 根目录下建立一个 sitemap.php 文件,添加代码如下

<?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="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">’
?>
<!– generated-on=<?php echo get_lastpostdate(‘blog’); ?>–>
  <url>
      <loc><?php echo ‘http://’.$_SERVER[‘HTTP_HOST’]; ?></loc>
      <mobile:mobile type="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>
      <lastmod><?php the_time(‘c’) ?></lastmod>
      <changefreq>monthly</changefreq>
      <priority>0.6</priority>
    </url>
  <url>
      <loc><?php the_permalink(); ?></loc>
      <mobile:mobile type="autoadapt"/>
      <lastmod><?php the_time(‘c’) ?></lastmod>
      <changefreq>monthly</changefreq>
      <priority>0.6</priority>
    </url>
<?php } ?>
<?php
/* 单页面 */
$mypages = get_pages();
if(count($mypages) > 0) {
    foreach($mypages as $page) { ?>
  <url>
      <loc><?php echo get_page_link($page->ID); ?></loc>
      <lastmod><?php echo str_replace(" ","T",get_page($page->ID)->post_modified); ?></lastmod>
      <changefreq>monthly</changefreq>
      <priority>0.6</priority>
    </url>
    <url>
      <loc><?php echo get_page_link($page->ID); ?></loc>
      <mobile:mobile type="autoadapt"/>
      <lastmod><?php echo str_replace(" ","T",get_page($page->ID)->post_modified); ?></lastmod>
      <changefreq>monthly</changefreq>
      <priority>0.6</priority>
  </url>
<?php }} /* 单页面循环结束 */ ?>
<?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>
      <changefreq>monthly</changefreq>
      <priority>0.6</priority>
    </url>
    <url>
      <loc><?php echo get_term_link($term, $term->slug); ?></loc>
      <mobile:mobile type="autoadapt"/>
      <changefreq>weekly</changefreq>
      <priority>0.8</priority>
  </url>
<?php }} /* 分类循环结束 */?>
<?php
 /* 标签 (可选) */
$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>
      <changefreq>monthly</changefreq>
      <priority>0.4</priority>
</url>
 <url>
      <loc><?php echo $link ?></loc>
      <mobile:mobile type="autoadapt"/>
      <changefreq>monthly</changefreq>
      <priority>0.4</priority>
  </url>
<?php  } /* 标签循环结束 */ ?>
</urlset>

第二步:添加伪静态
如果使用的是 lnmp 及 Nginx 服务器环境
添加网站的 Nginx 伪静态规则后平滑重启 nginx 即可(lnmp 怎么平滑重启 nginx?命令 lnmp nginx restart):
WordPress rewrite 重写规则配置文件位置在哪儿?(以军哥的 lnmp 为例)路径 /usr/local/nginx/conf/ 里面有个 WordPress.conf 便是
添加规则(使用 vim 或者 nano 编辑):

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

然后保存、重启 nginx

如果使用的 Apache 服务器
编辑网站根目录下的.htaccess 重写规则,加入如下规则:

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

同上步骤

添加好以上静态规则后,http://你的域名/sitemap.xml 显示正常就可以去百度提交了