查看: 31|回复: 0

[教程] wordpress非插件实现xml格式网站地图

[复制链接]

2

主题

0

回帖

0

积分

热心网友

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2011-11-17
发表于 2023-10-27 00:00:00 | 显示全部楼层 |阅读模式

废话不多说,直接上代码

  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:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance" rel="external nofollow" >http://www.w3.org/2001/XMLSchema-instance</a>" xmlns="<a href="http://www.sitemaps.org/schemas/sitemap/0.9" rel="external nofollow" rel="external nofollow" >http://www.sitemaps.org/schemas/sitemap/0.9</a>" 
  8. xsi:schemaLocation="<a href="http://www.sitemaps.org/schemas/sitemap/0.9" rel="external nofollow" rel="external nofollow" >http://www.sitemaps.org/schemas/sitemap/0.9</a> <a href="http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'" rel="external nofollow" >http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'</a>; 
  9. ?> 
  10. <!-- generated-on=<?php echo get_lastpostdate('blog'); ?>--> 
  11. <url> 
  12. <loc>http://localhost/</loc> 
  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文件并传至网站根目录

http://localhost/xmlmap.php

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部