隐私政策和版权声明

WordPress免插件生成网站地图(sitemap)

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="https://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="https://www.baidu.com/schemas/sitemap-mobile/1/">'
?>
  <url>
      <loc><?php echo get_home_url(); ?></loc>
      <lastmod><?php $ltime = get_lastpostmodified(GMT);$ltime = gmdate('Y-m-dTH:i:s+08: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>
<?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 the_time('c') ?></lastmod>
      <changefreq>weekly</changefreq>
      <priority>0.6</priority>
  </url>
<?php }} /* 单页面循环结束 */ ?> 
</urlset>

生成纯静态(sitemap.xml)

手动生成xml :
wget -O /var/www/html/wordpress/sitemap.xml --no-check-certificate https://www.cirno.site/sitemap.php >/dev/null 2>&1

放置到 crontab 中定时生成(以服务器时间,每个小时生成一次并覆盖):
0 */1 * * * wget -O /var/www/html/wordpress/sitemap.xml --no-check-certificate https://www.cirno.site/sitemap.php >/dev/null 2>&1

意义: 将 sitemap.php 放到某个目录,然后定时使用 wget 去请求这个文件,并将数据保存为 sitemap.xml 存放到网站根目录.
如果是启用了 https 的站点,需要加入 --no-check-certificate  的选项