---
title: "纯 PHP 代码生成 sitemap.xml 站点地图 - 一纸忘忧"
description: "站点地图可以方便网站管理员告诉搜索引擎它的网站上有哪些可供抓取的网页链接。最简单的站点地图形式，就是 sitemap.xml 文件，在其中列出网站中的网址以及关于每个网址的其他元数据（如上次更新的时间、更改的频率以及相对于网站上其他网址的重要程度），以便搜索引擎可以更加智能地抓取网站。"
canonical_url: "https://www.ikxin.com/article/wordpress-php-xml-sitemap"
last_updated: "2026-09-16T09:59:27.259Z"
locale: zh-cn
meta:
  "og:description": "站点地图可以方便网站管理员告诉搜索引擎它的网站上有哪些可供抓取的网页链接。最简单的站点地图形式，就是 sitemap.xml 文件，在其中列出网站中的网址以及关于每个网址的其他元数据（如上次更新的时间、更改的频率以及相对于网站上其他网址的重要程度），以便搜索引擎可以更加智能地抓取网站。"
  "og:image": "https://www.ikxin.com/_og/d/h_630%2Ca_~57qvIFBIUCDku6PnoIHnlJ~miJAgc2l0ZW1hcC54bWwg56uZ54K55Zyw5Zu-IC0g5LiA57q45b-Y5b-n%2Cc_Article%2Ctitle_~57qvIFBIUCDku6PnoIHnlJ~miJAgc2l0ZW1hcC54bWwg56uZ54K55Zyw5Zu-%2Cdescription_~56uZ54K55Zyw5Zu-5Y-v5Lul5pa55L6~572R56uZ566h55CG5ZGY5ZGK6K-J5pCc57Si5byV5pOO5a6D55qE572R56uZ5LiK5pyJ5ZOq5Lqb5Y-v5L6b5oqT5Y-W55qE572R6aG16ZO-5o6l44CC5pyA566A5Y2V55qE56uZ54K55Zyw5Zu-5b2i5byP77yM5bCx5pivIHNpdGVtYXAueG1sIOaWh-S7tu-8jOWcqOWFtuS4reWIl-WHuue9keermeS4reeahOe9keWdgOS7peWPiuWFs-S6juavj-S4que9keWdgOeahOWFtuS7luWFg-aVsOaNru-8iOWmguS4iuasoeabtOaWsOeahOaXtumXtOOAgeabtOaUueeahOmikeeOh-S7peWPiuebuOWvueS6jue9keermeS4iuWFtuS7lue9keWdgOeahOmHjeimgeeoi-W6pu-8ie-8jOS7peS-v-aQnOe0ouW8leaTjuWPr-S7peabtOWKoOaZuuiDveWcsOaKk-WPlue9keermeOAgg%2CsiteName_~5LiA57q45b-Y5b-n%2CsiteHost_www.ikxin.com%2Cp_Ii9hcnRpY2xlL3dvcmRwcmVzcy1waHAteG1sLXNpdGVtYXAi%2Cs_lP5yKDma-GKyzACw.png"
  "og:image:alt": "纯 PHP 代码生成 sitemap.xml 站点地图 - 一纸忘忧"
  "og:image:height": "630"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "纯 PHP 代码生成 sitemap.xml 站点地图"
  "twitter:image": "https://www.ikxin.com/_og/d/h_630%2Ca_~57qvIFBIUCDku6PnoIHnlJ~miJAgc2l0ZW1hcC54bWwg56uZ54K55Zyw5Zu-IC0g5LiA57q45b-Y5b-n%2Cc_Article%2Ctitle_~57qvIFBIUCDku6PnoIHnlJ~miJAgc2l0ZW1hcC54bWwg56uZ54K55Zyw5Zu-%2Cdescription_~56uZ54K55Zyw5Zu-5Y-v5Lul5pa55L6~572R56uZ566h55CG5ZGY5ZGK6K-J5pCc57Si5byV5pOO5a6D55qE572R56uZ5LiK5pyJ5ZOq5Lqb5Y-v5L6b5oqT5Y-W55qE572R6aG16ZO-5o6l44CC5pyA566A5Y2V55qE56uZ54K55Zyw5Zu-5b2i5byP77yM5bCx5pivIHNpdGVtYXAueG1sIOaWh-S7tu-8jOWcqOWFtuS4reWIl-WHuue9keermeS4reeahOe9keWdgOS7peWPiuWFs-S6juavj-S4que9keWdgOeahOWFtuS7luWFg-aVsOaNru-8iOWmguS4iuasoeabtOaWsOeahOaXtumXtOOAgeabtOaUueeahOmikeeOh-S7peWPiuebuOWvueS6jue9keermeS4iuWFtuS7lue9keWdgOeahOmHjeimgeeoi-W6pu-8ie-8jOS7peS-v-aQnOe0ouW8leaTjuWPr-S7peabtOWKoOaZuuiDveWcsOaKk-WPlue9keermeOAgg%2CsiteName_~5LiA57q45b-Y5b-n%2CsiteHost_www.ikxin.com%2Cp_Ii9hcnRpY2xlL3dvcmRwcmVzcy1waHAteG1sLXNpdGVtYXAi%2Cs_lP5yKDma-GKyzACw.png"
  "twitter:image:alt": "纯 PHP 代码生成 sitemap.xml 站点地图 - 一纸忘忧"
---

> Index of every page on this site: [llms.txt](https://www.ikxin.com/llms.txt)

# **纯 PHP 代码生成 sitemap.xml 站点地图**

2017.05.26 4,123  0

站点地图可以方便网站管理员告诉搜索引擎它的网站上有哪些可供抓取的网页链接。最简单的站点地图形式，就是 `sitemap.xml` 文件，在其中列出网站中的网址以及关于每个网址的其他元数据（如上次更新的时间、更改的频率以及相对于网站上其他网址的重要程度），以便搜索引擎可以更加智能地抓取网站。

对于 WordPress 生成 `sitemap.xml` 地图，国内已经有人写了专门的插件，比如较为知名的柳城的 **Baidu Sitemap Generator**，国外的有 **Google XML Sitemaps**。

不过，我感觉仅仅为了生成一个 `sitemap.xml` 文件就使用插件会显得有点浪费资源，所以就用代码实现吧！代码是从网上找到的，我稍作修改后，支持生成标签和分类的 XML，并增加了 `sitemap.xml` 生成时间。

## PHP 代码

新建一个 `sitemap.php` 文件，将以下代码写入该文件：

 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/">';
?>
<!-- 生成时间 <?php echo $showtime = date("Y-m-d H:i:s"); ?> -->
<url>
    <loc><?php echo get_home_url(); ?></loc>
    <lastmod><?php $ltime = get_lastpostmodified(GMT); $ltime = gmdate('Y-m-d\TH: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>weekly</changefreq>
    <priority>0.8</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); ?>+00:00</lastmod>
    <changefreq>weekly</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>weekly</changefreq>
    <priority>0.4</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>weekly</changefreq>
    <priority>0.2</priority>
</url>
<?php } ?>
</urlset>
```

将 `sitemap.php` 上传到网站根目录，然后访问查看效果。

## 伪静态方案

### Nginx

编辑已存在的 Nginx 伪静态规则，新增如下规则后重启 Nginx 即可：

 Nginx

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

### Apache

编辑网站根目录的 `.htaccess` 文件，加入如下规则：

 Apache

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

## 纯静态方案

**Linux 定时任务 + wget 定时生成 `sitemap.xml`（该方案适合 VPS 用户，不适用于虚拟主机）**

将网站根目录的 `sitemap.php` 重命名为一个只有自己知道的 PHP 文件，比如 `xml.php`。

然后用 Linux 定时任务执行以下命令，将数据保存为 `sitemap.xml`，存放到网站根目录即可：

 Bash

```
wget -O /home/www/www.ikxin.com/sitemap.xml https://www.ikxin.com/sitemap.php
```

- `https://www.ikxin.com/sitemap.php` 是 PHP 的路径。
- `/home/www/www.ikxin.com/sitemap.xml` 是 XML 文件存放路径。

这样一来，就解决了 `sitemap.xml` 是动态数据的问题了。

## **评论**

(0)

暂无评论，来说两句吧

## **发布评论**

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://www.ikxin.com/#website","@type":"WebSite","description":"一纸忘忧的个人网站，关于技术探索与日常生活，记录 Web 开发、服务端与 DevOps 实践，分享软件工具、数码体验，以及代码之外的见闻与思考。","inLanguage":"zh-CN","name":"一纸忘忧","url":"https://www.ikxin.com/","workTranslation":[{"@id":"https://www.ikxin.com/zh-tw#website"},{"@id":"https://www.ikxin.com/ko#website"},{"@id":"https://www.ikxin.com/en#website"},{"@id":"https://www.ikxin.com/ja#website"}]},{"@id":"https://www.ikxin.com/article/wordpress-php-xml-sitemap#webpage","@type":"WebPage","description":"站点地图可以方便网站管理员告诉搜索引擎它的网站上有哪些可供抓取的网页链接。最简单的站点地图形式，就是 sitemap.xml 文件，在其中列出网站中的网址以及关于每个网址的其他元数据（如上次更新的时间、更改的频率以及相对于网站上其他网址的重要程度），以便搜索引擎可以更加智能地抓取网站。","name":"纯 PHP 代码生成 sitemap.xml 站点地图","url":"https://www.ikxin.com/article/wordpress-php-xml-sitemap","isPartOf":{"@id":"https://www.ikxin.com/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://www.ikxin.com/article/wordpress-php-xml-sitemap"]}]}]}
```

## Sitemap

See the full [sitemap](https://www.ikxin.com/sitemap.md) for all pages.
