wordpress通过Linux shell脚本自动生成sitemap.xml
1.在网站根目录(为了安全起见,建议放到一个不会轻易让人找到的路径下)下新建sitemap.php文件,内容如下:
<?php
require('./wp-blog-header.php'); //注意:此处为相对路径,根据实际情况填写
header("Content-type: text/xml");
header('HTTP/1.1 200 OK');
$posts_to_show = 1000; // 获取1000篇文章
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
?>
<url>
<loc>http://www.cenliming.com/</loc>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<?php
header("Content-type: text/xml");
$myposts = get_posts( "numberposts=" . $posts_to_show );
foreach( $myposts as $post ) { ?>
<url>
<loc><?php the_permalink(); ?></loc>
<changefreq>monthly</changefreq>
<priority>0.6</priority>
</url>
<?php } // end foreach ?>
</urlset>
然后保存,退出。
2.然后在网站根目录创建sitemap.xml文件:
touch sitemap.xml
3.crontab 、wget 命令将网站地图内容输出到sitemap.xml中:
crontab -e 0 5 * * * wget -O /xxx/xxx/web/sitemap.xml http://www.cenliming.com/sitemap.php (注:/xxx/xxx/web/为网站根目录)
这样,就可以每天凌晨5点自动的生成网站地图数据了。
关注公众号:拾黑(shiheibook)了解更多
[广告]赞助链接:
四季很好,只要有你,文娱排行榜:https://www.yaopaiming.com/
让资讯触达的更精准有趣:https://www.0xu.cn/
关注网络尖刀微信公众号随时掌握互联网精彩
赞助链接
排名
热点
搜索指数
- 1 习近平将发表二〇二六年新年贺词 7904141
- 2 2026年国补政策来了 7808738
- 3 东部战区:开火!开火!全部命中! 7712893
- 4 2026年这些民生政策将惠及百姓 7616985
- 5 小学食堂米线过期2.5小时被罚5万 7519709
- 6 解放军喊话驱离台军 原声曝光 7428214
- 7 为博流量直播踩烈士陵墓?绝不姑息 7327605
- 8 每月最高800元!多地发放养老消费券 7238391
- 9 数字人民币升级 1月1日起将计付利息 7141831
- 10 2026年1月1日起 一批新规将施行 7040675






![小云云Cloudy_LYY徒步登顶,菩萨应该听到了[心] ](https://imgs.knowsafe.com:8087/img/aideep/2026/4/8/abfcdd930f434248e9ac59e0eae003ad.jpg?w=250)

程序猿
