WordPress纯代码建立HTML,XML格式网站地图

admin wordpress评论666字数 1297阅读模式

xml地图的创建方法就更简单了,新建一个名为xmlmap.php的文件,然后填入以下代码:

  1. <?php  
  2. include '/includes/connection.php';  
  3. require('./wp-blog-header.php');  
  4. header("Content-type: text/xml");  
  5. header('HTTP/1.1 200 OK');  
  6. $posts_to_show = 1000// 获取文章数量  
  7. echo '<?xml version="1.0" encoding="UTF-8"?>';  
  8. echo '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"  
  9. xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';  
  10. ?>  
  11. <!-- generated-on=<?php echo get_lastpostdate('blog'); ?>-->  
  12.   <url>  
  13.       <lastmod><?php echo get_lastpostdate('blog'); ?></lastmod>  
  14.       <changefreq>daily</changefreq>  
  15.       <priority>1.0</priority>  
  16.   </url>  
  17. <?php  
  18. header("Content-type: text/xml");  
  19. $myposts = get_posts( "numberposts=" . $posts_to_show );  
  20. foreach( $myposts as $post ) { ?>  
  21.   <url>  
  22.       <loc><?php the_permalink(); ?></loc>  
  23.       <lastmod><?php the_time('c') ?></lastmod>  
  24.       <changefreq>monthly</changefreq>  
  25.       <priority>0.6</priority>  
  26.   </url>  
  27. <?php } // end foreach ?>  
  28. </urlset>  

将xmlmap.php文件传至网站根目录,然后根据环境写好url转发规则。

首先是apache下的规则:

  1. RewriteEngine On  
  2. RewriteBase /  
  3. RewriteRule ^sitemap.  

将以上代码加入到.htaccess文件即可,接下来是nginx下规则:

  1. rewrite ^/sitemap.xml$ /xmlmap.php;  

更多相关文章

wordpress添加数据库查询次数及用时

版权声明:文章图片资源来源于网络,如有侵权,请留言删除!!!
admin
  • 本文由 发表于 2021年9月25日 17:34:35
  • 转载请务必保留本文链接:https://www.58pxe.com/7677.html
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: