优选主流主机商
任何主机均需规范使用

WordPress无插件创建XML网站地图:优化SEO与提升搜索引擎排名的实战教程

废话不多说,直接上代码

  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

未经允许不得转载:搬瓦工中文网 » WordPress无插件创建XML网站地图:优化SEO与提升搜索引擎排名的实战教程