WordPress免插件生成完整站点地图(sitemap.xml)的php代码

WordPress免插件生成完整站点地图(sitemap.xml)的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 get_home_url(); ?></loc>
      <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>
<?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.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>
<?php  } /* 标签循环结束 */ ?>
</urlset>

将以上代码保存为sitemap.php,传到网站根目录。

二、伪静态

①、Nginx

编辑已存在的Nginx伪静态规则,新增如下规则后(平滑)重启nginx即可:

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

②、Apache

编辑网站根目录的 .htaccess ,加入如下规则:

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

做好伪静态规则后,就可以直接访问sitemap.xml看看效果了,比如 http://xoin.me/sitemap.xml

三、纯静态

看到很多朋友已经在问这个sitemap如何静态化,加快打开速度。毕竟每次重新生成绝对是一个耗能大户,而且还有可能被有心之人拿来作为攻击入口!

其实,张戈博客早就已经实现sitemap.xml静态化了,而且在后面的文章中也有提到=>【相关文章

实现方法有多种,比如在Nginx的fastcgi缓存中取消xml文件的缓存屏蔽,或者使用张戈博客最早使用的php生成静态文件等。

在这里,我就分享一个自己一直在用的最简单的实现方法:Linux定时任务+wget定时生成sitemap.xml

具体实现:将sitemap.php放到某个不为人知的目录,然后定时使用wget去请求这个文件,并将数据保存为sitemap.xml存放到网站根目录就可以了!比如:

#每天在网站根目录生成一个sitemap.xml
wget –O /home/wwwroot/blog.supertyler.com/sitemap.xml https://blog.supertyler.com/diypath/sitemap.php  >/dev/null 2>&1

如果是启用了https的站点,需要加入 –no-check-certificate 的选项,即:

#每天在网站根目录生成一个sitemap.xml 针对https网站)
wget –O /home/wwwroot/blog.supertyler.com/sitemap.xml —no–check–certificate https://blog.supertyler.com/diypath/sitemap.php  >/dev/null 2>&1

这样一来,就解决了sitemap.xml是动态数据问题了!

原文转自张戈博客,有删改。

JavaScript前端开发

手写一个Promise

2022-1-9 11:31:49

PHP后端开发

WordPress免插件生成完整站点地图(sitemap.xml)的php代码

2018-2-2 9:05:53

搜索