准备工作
本方法通过在 WordPress 根目录创建一个 PHP 文件,并配置服务器重写规则,实现无需插件即可生成符合百度标准的 sitemap.xml 文件。适用于 Nginx 和 Apache 服务器环境。
第一步:创建 sitemap.php 文件
在 WordPress 网站的根目录(即与 wp-config.php 同级的目录)下,创建一个名为 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>
代码说明与注意事项:
- 此代码会生成包含首页、文章、页面、分类和标签的站点地图,并为每种内容类型生成了标准版和移动适配版两个条目,以符合百度移动适配要求。
$posts_to_show = 1000;限制了生成的文章数量,可根据需要调整。如果文章超过1000篇,建议分页处理或提高此数值。- 代码中部分日期格式处理(如
str_replace(" ","T",...))可能不够严谨,但通常能工作。更推荐使用 WordPress 的日期格式化函数。 - 创建文件后,请确保其权限正确(通常为644),以便Web服务器能够读取。
第二步:配置服务器重写规则
为了让访问 http://你的域名/sitemap.xml 时能正确执行 sitemap.php 文件,需要配置服务器的 URL 重写规则。
Nginx 服务器配置
如果你使用的是 Nginx 服务器(例如 LNMP 环境),需要编辑网站的 Nginx 配置文件。
-
找到配置文件:通常位于
/usr/local/nginx/conf/vhost/你的域名.conf或/etc/nginx/sites-available/目录下。如果使用了 LNMP 一键包,也可能在/usr/local/nginx/conf/wordpress.conf中定义通用规则。 -
添加重写规则:在
server { ... }块内,找到处理 WordPress 的重写规则部分(通常以location / { ... }开头),并在其前面或后面添加以下规则:rewrite ^/sitemap.xml$ /sitemap.php last; -
重启 Nginx:保存配置文件后,执行命令重启 Nginx 使配置生效。
- LNMP 一键包:
lnmp nginx restart - 通用系统:
systemctl restart nginx或service nginx restart
- LNMP 一键包:
Apache 服务器配置
如果你使用的是 Apache 服务器,配置更为简单。
-
编辑 .htaccess 文件:在 WordPress 网站的根目录下,找到
.htaccess文件(如果隐藏,请显示隐藏文件)。 -
添加重写规则:在
# BEGIN WordPress规则块之前或之后,添加以下规则:RewriteRule ^sitemap.xml$ sitemap.php [L] -
无需重启:Apache 的 .htaccess 文件修改后通常立即生效。如果未生效,请检查 Apache 是否启用了
mod_rewrite模块,并确保.htaccess文件权限允许读取。
测试与提交
完成以上两步后,在浏览器中访问 http://你的域名/sitemap.xml。如果配置正确,你应该能看到一个格式良好的 XML 站点地图,而不是文件下载或错误页面。
确认无误后,即可登录 百度搜索资源平台,将你的 sitemap.xml 地址提交给百度,以帮助搜索引擎更好地抓取和索引你的网站内容。
注:本文方法基于网络分享代码,适用于常规 WordPress 站点。对于大型站点或特殊需求,建议评估性能并考虑使用更专业的站点地图解决方案。