免插件为 WordPress 添加 Xml 网站地图

自从萌生了辞职的想法后,这两天日日在思考辞职之后我该何去何从,要不要重拾网赚呢?因此我也在恶补这个行业的相关资讯,期间读到 Google 发布的《谷歌搜索引擎优化初学者指南》,里头提到了网站地图(Sitemap)的重要性…于是便有了此文。

将下列代码保存为 sitemap.php 放在网站根目录,这时候可以试试打开你的 博客主页/sitemap.php 看看是否添加成功。

<?php
require('./wp-blog-header.php');
header("Content-type: text/xml");
header('HTTP/1.1 200 OK');
$posts_to_show = 1000; 
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">'
?>
<!-- generated-on=<?php echo get_lastpostdate('blog'); ?> 牧羊人(http://www.shephe.com) -->
  <url>
      <loc><?php echo get_home_url(); ?></loc>
      <lastmod><?php $ltime = get_lastpostmodified(GMT);$ltime = gmdate('Y-m-d\TH:i:s+00:00', strtotime($ltime)); echo $ltime; ?></lastmod>
      <changefreq>daily</changefreq>
      <priority>1.0</priority>
  </url>
<?php
/* 文章页面 */ 
header("Content-type: text/xml");
$myposts = get_posts( "numberposts=" . $posts_to_show );
foreach( $myposts as $post ) { ?>
  <url>
      <loc><?php the_permalink(); ?></loc>
      <lastmod><?php the_time('c') ?></lastmod>
      <changefreq>monthly</changefreq>
      <priority>0.6</priority>
  </url>
<?php } /* 文章循环结束 */ ?>  
<?php
/* 单页面 */ 
$mypages = get_pages();
if(count($mypages) > 0) {
    foreach($mypages as $page) { ?>
    <url>
      <loc><?php echo get_page_link($page->ID); ?></loc>
      <lastmod><?php echo str_replace(" ","T",get_page($page->ID)->post_modified); ?>+00:00</lastmod>
      <changefreq>weekly</changefreq>
      <priority>0.6</priority>
  </url>
<?php }} /* 单页面循环结束 */ ?> 
<?php
/* 博客分类 */ 
$terms = get_terms('category', 'orderby=name&hide_empty=0' );
$count = count($terms);
if($count > 0){
foreach ($terms as $term) { ?>
    <url>
      <loc><?php echo get_term_link($term, $term->slug); ?></loc>
      <changefreq>weekly</changefreq>
      <priority>0.8</priority>
  </url>
<?php }} /* 分类循环结束 */?> 
<?php
 /* 标签(可选) */
$tags = get_terms("post_tag");
foreach ( $tags as $key => $tag ) {
	$link = get_term_link( intval($tag->term_id), "post_tag" );
	     if ( is_wp_error( $link ) )
		  return false;
		  $tags[ $key ]->link = $link;
?>
 <url>
      <loc><?php echo $link ?></loc>
      <changefreq>monthly</changefreq>
      <priority>0.4</priority>
  </url>
<?php  } /* 标签循环结束 */ ?> 
</urlset>

如果能正常显示 XML 格式网页则表示代码添加无误,这时候只需要将 博客首页/sitemap.php 重定向到 博客首页/sitemap.xml,具体是修改位于根目录下的 .htaccess 添加语句 RewriteRule ^(sitemap)\.xml$ $1.php,一般情况下应该是这样子的:

RewriteRule ^(sitemap)\.xml$ $1.php
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

以上是 apache 的写法,nginx 是在配置文件的 server 块中写入下面的一句:

rewrite /sitemap.xml /sitemap.php last;
1920 1280 Kevin's
「免插件为 WordPress 添加 Xml 网站地图」有 28 条评论
  • chencool
    05/03/2017 at 13:49 回复

    代码不错,适合折腾~

  • 老杨
    04/06/2017 at 11:51 回复

    直接插件省事。

    • 牧羊人
      04/08/2017 at 16:24 回复

      你这么喜欢折腾的还用插件啊哈哈哈哈

      • 老杨
        04/08/2017 at 23:19 回复

        感觉这个插件生成的会标准点。

  • Naifu
    04/01/2017 at 16:59 回复

    还是wp好啊,各种扩展,各种get,我一般都是用在线网站生成sitemap

    • 牧羊人
      04/04/2017 at 09:03 回复

      还能在线生成??? 😯

  • masy
    03/31/2017 at 09:14 回复

    博客折腾的不错

  • harries
    03/30/2017 at 23:56 回复

    已添加

    • 牧羊人
      04/04/2017 at 09:02 回复

      你的粘牛鼻啊,大站呐! 😛

  • 菊部
    03/30/2017 at 20:33 回复

    之前在张戈那里看到类似的 有空试试

    • 牧羊人
      04/04/2017 at 08:59 回复

      可以的

      很简单的操作,如果需要的话~

  • 企业管理培训
    03/29/2017 at 11:08 回复

    其实没这么复杂

  • muguayuan.com
    03/28/2017 at 19:20 回复

    感谢,终于可以干掉一个插件了

    • 牧羊人
      04/04/2017 at 08:53 回复

      哈哈哈哈

      欢迎使用~

  • 夏天烤洋芋
    03/28/2017 at 16:23 回复

    老哥,来和我学网赚吧。日赚500. 😆

    • 牧羊人
      04/04/2017 at 08:51 回复

      牛鼻啊,日赚500!

  • Loekman
    03/28/2017 at 13:12 回复

    可是这个有什么用呢?对推广有用吗

    • 牧羊人
      04/04/2017 at 08:48 回复

      对推广肯定是有用的,这就相当于明确告诉 搜索引擎 你的站点有哪些链接,以更好抓取。

      不过对于咱们这种小博客来讲就不是那么必要了,只能说是增加站点完整性吧~

  • WordPress头条
    03/27/2017 at 14:27 回复

    赶紧辞职,不要犹豫。湖边小屋就在前方。

    • 牧羊人
      04/04/2017 at 08:46 回复

      说的楞个轻巧~

  • Life in Faw
    03/27/2017 at 12:31 回复

    很想学习一下这些知识,自己学习感觉好慢。

    • 牧羊人
      04/04/2017 at 08:44 回复

      这些都是非常简单的,比如我虽然没系统学过,但是看看帖子也就明白了~ 😉

  • 胡德杰
    03/26/2017 at 11:04 回复

    其实用插件是很好的!不使用插件我觉得某种程度上算是自断手脚吧!

    • 牧羊人
      04/04/2017 at 09:07 回复

      对对对对,

      所以我目前一个插件都没用哈哈哈哈

  • 神父
    03/25/2017 at 22:46 回复

    辞职,这是个很霸气的举动

    • 牧羊人
      04/04/2017 at 09:07 回复

      最终也没有辞职,可能是胆儿太小了吧。。

  • 文栋说自媒体
    03/25/2017 at 22:37 回复

    还是插件用起来方便

    • 牧羊人
      04/04/2017 at 09:06 回复

      有一种总想把别人的东西集成到自己身上来的感觉哈哈哈~

发表评论

请输入关键词…