wordpress代码实现网站地图sitemap的html和xml的方法

今天介绍的就是不使用插件完成wordpress代码实现网站地图sitemap的html和xml的方法首先我先提供下sitemap.php的文件吧: 将文件下载下来,上传到当前文件夹的根目录,然后新建页面,选择站点地图模板即可!这样一个html的站点地图就OK了。在空间wordpress的根目录下创建xmlmap.php文件

  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="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
  8. xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
  9. ?>
  10. <!-- generated-on=<?php echo get_lastpostdate('blog'); ?>-->
  11.   <url>
  12.       <loc>http://www.2zzt.com/</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>

上传到根目录后,就是设置url转发规则了,目的是让http://www.2zzt.com/sitemap.xml能够被访问,当然这个sitemap.xml内容就是xmlmap.php的

根据不同的服务器环境来设置url转发规则!

首先是apache下的规则:

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

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

  1. rewrite ^/sitemap.xml$ /xmlmap.php;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值