WordPress 免插件生成完整站点地图

Admin
Admin
Admin
52
文章
6
评论
2018年3月1日11:14:48 评论 10,327 2704字阅读9分0秒

站点地图sitemap.xml)就是方便快捷的给搜索引擎蜘蛛指明道路的一个方式,WordPress 中有很多插件来生成站点地图(sitemap.xml)的,但是大家都知道 WordPress 本身就是很臃肿的了,插件安装多了会影响网站的响应速度,所以有些功能用纯代码实现的尽量不要使用插件,网上这类的代码也是很多的,这边我就引入一下前辈的方法自己在做一些总结。

一、PHP 代码

首先将以下代码保存为 sitemap.php,然后将这个.php文件放到网站根目录下(网站根目录是指和 wp-admin、wp-content 同一级目录),然后在后台新建页面,模版选择站点地图,在外观 -- 菜单中将该页面加入自己想要显示的位置就行了。

  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'); ?>-->
  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>

以上代码中将 博客单页面、分类、标签也都生成了,本博客只是将文章页生成了 xml 内容,大家可以根据自己的想法做修改。

二、设置伪静态

1、Nginx 服务器
找到你的 Nginx 的conf配置文件,在 location 中加上以下的一条规则,然后重启 Nginx 服务

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

2、Apache 服务器
在你的网站根目录中找到 .htaccess 文件,在最后加上以下的一条规则,然后重启 Apache 服务

  1. RewriteRule ^(sitemap)\.xml$ $1.php

具体效果见本站底部 网站地图XML网站地图

三、总结

注:如果以上步骤操作成功后,没有生成 sitemap.xml 文件,那么你就手动在你的网站根目录下创建一个 sitemap.xml 文件,然后在试试效果。

博主只是一名前端的小白,只是把自己用到的知识分享一下,要是有什么不对的地方,欢迎大家提出~~

继续阅读
Admin
  • 本文由 发表于 2018年3月1日11:14:48
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
移除 SourceTree 无用的远程分支 资源分享

移除 SourceTree 无用的远程分支

在开发过程中我们很多时候都需要将当前分支推送到远程仓库,该分支完成其使命之后再将其删除。 使用 SourceTree 的同学可能会发现 SourceTree 追踪的远端分支越来越多,许多已经删除的分支...
Linux 服务器修改 ssh 默认 22 端口方法 资源分享

Linux 服务器修改 ssh 默认 22 端口方法

博主在之前腾讯云有活动的时候,花了300多大洋撸了一台三年时间的 1H2G 入门级服务器,当时每次用 Shell 工具连接的时候都是感觉连接速度特别慢,等连接上后会有一个提示 “There were ...
WordPress 伪静态规则设置方法 资源分享

WordPress 伪静态规则设置方法

众所周知 WordPress 默认的链接是动态链接,虽然现在搜索引擎对动态的链接也有着非常好的收录,但是基于链接的美观,以及伪静态对搜索引擎更加有好的原因,我们还是非常希望能够将我们的 WordPre...
常见 HTTP 状态码的理解 资源分享

常见 HTTP 状态码的理解

当浏览者访问一个网页时,浏览者的浏览器会向网页所在服务器发出请求,当浏览器接收并显示网页前,此网页所在的服务器会返回一个包含 HTTP 状态码的信息头(server header)用以响应浏览器的请求...
匿名

发表评论

匿名网友 填写信息

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