WordPress纯代码生成XML网站地图

  1. <?php  
  2. require('./wp-blog-header.php');  
  3. header("Content-type: text/xml");  
  4. header('HTTP/1.1 200 OK');  
  5. $posts_to_show = 1000;  
  6. echo '<?xml version="1.0" encoding="UTF-8"?>';  
  7. echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">'  
  8. ?>  
  9. <!-- generated-on=<?php echo get_lastpostdate('blog'); ?>  By 老梁`s Blog(https://www.laoliang.net)-->  
  10. <url>  
  11. <loc><?php echo get_home_url(); ?></loc>  
  12. <lastmod><?php $ltime = get_lastpostmodified(GMT);$ltime = gmdate('Y-m-d\TH:i:s+00:00', strtotime($ltime)); echo $ltime; ?></lastmod>  
  13. <changefreq>daily</changefreq>  
  14. <priority>1.0</priority>  
  15. </url>  
  16. <?php  
  17. /* 文章页面 */  
  18. $myposts = get_posts( "numberposts=" . $posts_to_show );  
  19. foreach$myposts as $post ) { ?>  
  20. <url>  
  21. <loc><?php the_permalink(); ?></loc>  
  22. <lastmod><?php the_time('c') ?></lastmod>  
  23. <changefreq>monthly</changefreq>  
  24. <priority>0.6</priority>  
  25. </url>  
  26. <?php } /* 文章循环结束 */ ?>  
  27. <?php  
  28. /* 单页面 */  
  29. $mypages = get_pages();  
  30. if(count($mypages) > 0) {  
  31. foreach($mypages as $page) { ?>  
  32. <url>  
  33. <loc><?php echo get_page_link($page->ID); ?></loc>  
  34. <lastmod><?php echo str_replace(" ","T",get_page($page->ID)->post_modified); ?>+00:00</lastmod>  
  35. <changefreq>weekly</changefreq>  
  36. <priority>0.6</priority>  
  37. </url>  
  38. <?php }} /* 单页面循环结束 */ ?>  
  39. <?php  
  40. /* 博客分类 */  
  41. $terms = get_terms('category', 'orderby=name&hide_empty=0' );  
  42. $count = count($terms);  
  43. if($count > 0){  
  44. foreach ($terms as $term) { ?>  
  45. <url>  
  46. <loc><?php echo get_term_link($term$term->slug); ?></loc>  
  47. <changefreq>weekly</changefreq>  
  48. <priority>0.8</priority>  
  49. </url>  
  50. <?php }} /* 分类循环结束 */?>  
  51. <?php  
  52. /* 标签(可选) */  
  53. $tags = get_terms("post_tag");  
  54. foreach ( $tags as $key => $tag ) {  
  55. $link = get_term_link( intval($tag->term_id), "post_tag" );  
  56. if ( is_wp_error( $link ) )  
  57. return false;  
  58. $tags$key ]->link = $link;  
  59. ?>  
  60. <url>  
  61. <loc><?php echo $link ?></loc>  
  62. <changefreq>monthly</changefreq>  
  63. <priority>0.4</priority>  
  64. </url>  
  65. <?php } /* 标签循环结束 */ ?>  
  66. </urlset>  

用法

1.保存这个页面文件,命名为sitemap.php;
2.将sitemap.php文件上传到网站空间根目录下
3.根据不同的服务器环境来设置url转发规则!
首先是apache下的规则:

  1. RewriteEngine On  
  2. RewriteBase /  
  3. RewriteRule ^sitemap.xml$ xmlmap.php                      

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

  1. rewrite ^/sitemap.xml$ /xmlmap.php;                   
原文链接:https://www.artcg.design/273.html,转载请注明出处。
0

评论0

请先

站点公告

  1. 1.请不要使用QQ浏览器有可能无法访问.
  2. ————————————————
  3. 2.邮箱注册时,验证码可能归类在垃圾箱里,请注意查收
  4. ————————————————
  5. 3.如果有问题,请添加微信qifengct客服QQ:1924173440咨询
  6. ————————————————
显示验证码
没有账号?注册  忘记密码?