网站地图sitemap 纯代码制作html和xml版本——WordPress教程

2017年10月15日21:58:29 18 7,325
摘要

站点地图sitemap 版本分为xml版本和html版本,sitemap xml版本主要是提供给搜索引擎看得。而html版本作用是提高用户体验,让 用户一眼就能看清楚网站内容和分类。这篇文章主要教大家怎样在不使用插件的情况下制作站点地图

 

网站地图sitemap 纯代码制作html和xml版本——WordPress教程

 

一、制作html版本的站点地图

将下面的代码保存为 sitemap.php 并上传到主题目录

  1. <?php
  2. /*
  3.  Template Name: Sitemap
  4. */
  5. ?>
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  7. <html xmlns="http://www.w3.org/1999/xhtml">
  8. <head profile="http://gmpg.org/xfn/11">
  9. <meta http-equiv="Content-Type" content="text/html; charset=<?php bloginfo( 'charset' ); ?>" />
  10. <title>站点地图 - <?php bloginfo('name'); ?></title>
  11. <meta name="keywords" content="站点地图,<?php bloginfo('name'); ?>" />
  12. <meta name="copyright" content="<?php bloginfo('name'); ?>" />
  13. <link rel="canonical" href="<?php echo get_permalink(); ?>" />
  14. <style type="text/css">
  15.     body {font-family: Microsoft Yahei,Verdana;font-size:13px;margin:0 auto;color: #000000;background: #ffffff;width: 990px;margin: 0 auto}
  16.     a:link,a:visited {color:#000;text-decoration:none;}
  17.     a:hover {color:#08d;text-decoration:none;}
  18.     h1,h2,h3,h4,h5,h6 {font-weight:normal;}
  19.     img {border:0;}
  20.     li {margin-top: 8px;}
  21.     .page {padding: 4px; border-top: 1px #EEEEEE solid}
  22.     .author {background-color:#EEEEFF; padding: 6px; border-top: 1px #ddddee solid}
  23.     #nav, #content, #footer {padding: 8px; border: 1px solid #EEEEEE; clear: both; width: 95%; margin: auto; margin-top: 10px;}
  24. </style>
  25. </head>
  26. <body vlink="#333333" link="#333333">
  27. <h2 style="text-align: center; margin-top: 20px"><?php bloginfo('name'); ?>'s SiteMap </h2>
  28. <center></center>
  29. <div id="nav"><a href="<?php bloginfo('url'); ?>/"><strong><?php bloginfo('name'); ?></strong></a> &raquo; <a href="<?php echo get_permalink(); ?>">站点地图</a></div>
  30. <div id="content">
  31. <h3>最新文章</h3>
  32. <ul>
  33. <?php
  34. $previous_year = $year = 0;
  35. $previous_month = $month = 0;
  36. $ul_open = false;
  37. $myposts = get_posts('numberposts=-1&orderby=post_date&order=DESC');
  38. foreach($myposts as $post) :
  39. ?>
  40. <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
  41. <?php endforeach; ?>
  42. </ul>
  43. </div>
  44. <div id="content">
  45. <li class="categories">分类目录<ul>
  46. <?php wp_list_categories('title_li='); ?>
  47. </ul></li>
  48. </div>
  49. <div id="content">
  50. <li class="categories">单页面</li>
  51. <?php wp_page_menu( $args ); ?>
  52. </div>
  53. <div id="footer">查看博客首页: <strong><a href="<?php bloginfo('url'); ?>/"><?php bloginfo('name'); ?></a></strong></div><br />
  54. <center>
  55. <div style="text-algin: center; font-size: 11px"><strong><a href="http://www.timle.cn/sitemap_baidu.xml" target="_blank">Baidu-SiteMap</a></strong> Latest Update: <?php $last = $wpdb->get_results("SELECT MAX(post_modified) AS MAX_m FROM $wpdb->posts WHERE (post_type = 'post' OR post_type = 'page') AND (post_status = 'publish' OR post_status = 'private')");$last = date('Y-m-d G:i:s', strtotime($last[0]->MAX_m));echo $last; ?><br /><br /></div>
  56. </center>
  57. </body>
  58. </html>

 

打开wordpress后台,新建页面,模板选择"sitemap",发布。记住这个页面的链接,将这个链接放到footer或者其他地方。

 

网站地图sitemap 纯代码制作html和xml版本——WordPress教程

 

二、制作xml版本的站点地图

将下面代码保存为 sitemap.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="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. 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 } /* 文章循环结束 */ ?>
  28. <?php
  29. /* 单页面 */
  30. $mypages = get_pages();
  31. if(count($mypages) > 0) {
  32.     foreach($mypages as $page) { ?>
  33.     <url>
  34.       <loc><?php echo get_page_link($page->ID); ?></loc>
  35.       <lastmod><?php echo str_replace(" ","T",get_page($page->ID)->post_modified); ?>+00:00</lastmod>
  36.       <changefreq>weekly</changefreq>
  37.       <priority>0.6</priority>
  38.   </url>
  39. <?php }} /* 单页面循环结束 */ ?>
  40. <?php
  41. /* 博客分类  www.stcash.com*/
  42. $terms = get_terms('category', 'orderby=name&hide_empty=0' );
  43. $countcount = count($terms);
  44. if($count > 0){
  45. foreach ($terms as $term) { ?>
  46.     <url>
  47.       <loc><?php echo get_term_link($term$term->slug); ?></loc>
  48.       <changefreq>weekly</changefreq>
  49.       <priority>0.8</priority>
  50.   </url>
  51. <?php }} /* 分类循环结束 */?>
  52. <?php
  53.  /* 标签(可选) */
  54. $tags = get_terms("post_tag");
  55. foreach ( $tags as $key => $tag ) {
  56.     $link = get_term_link( intval($tag->term_id), "post_tag" );
  57.          if ( is_wp_error( $link ) )
  58.           return false;
  59.           $tags$key ]->link = $link;
  60. ?>
  61.  <url>
  62.       <loc><?php echo $link ?></loc>
  63.       <changefreq>monthly</changefreq>
  64.       <priority>0.4</priority>
  65.   </url>
  66. <?php  } /* 标签循环结束 */ ?>
  67. </urlset>

 

重写.htaccess,在第一行加上一句代码

此处为隐藏的内容!
发表评论并刷新,方可查看

这样代码的意思是将php文件重写为xml文件,ok,xml制作完毕。

 

至此,xml版本的html版本的站点地图制作成功。提醒大家在制作过程中注意两个sitemap.php上传的目录是不一样的,不要弄混淆。

制作好以后,我们还可以使用百度种子页面提高收录。

 

 

若文章图片、下载链接等信息出错,请在评论区留言反馈,博主将第一时间更新!如果喜欢,请打赏支持本站,谢谢大家!

发表评论

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

目前评论:18   其中:访客  0   博主  0

    • zero zero 0 来自天朝的朋友 上海市 移动

      看看

      • 星光:bg4pgr.com 星光:bg4pgr.com 4 来自天朝的朋友 山东省潍坊市 移动

        看看

        • 星光xgboke.com 星光xgboke.com 4 来自天朝的朋友 山东省潍坊市 移动

          再看下

          • 星光xgboke.com 星光xgboke.com 4 来自天朝的朋友 山东省潍坊市 移动

            重写.htaccess,在第一行加上一句代码 博主 这段话一直不明白是什么意思 只能使用插件 能否说下具体在哪里使用 htaccess 第一行是哪里 能给个截图看看吗 我是阿里云虚拟主机

            • 老豆子 老豆子 2 来自天朝的朋友 陕西省汉中市 电信

              很溜

              • voice站点 voice站点 3 来自天朝的朋友 四川省成都市新都区 电信

                我用的是Linux,但是不知道怎么修改NGINX

                • 拉布杜拉君 拉布杜拉君 1 来自天朝的朋友 福建省福州市 电信

                  看看

                  • 3K分享网 3K分享网 0 来自天朝的朋友 QQ浏览器 Windows 7 湖北省武汉市 联通

                    谢谢分享

                    • Langoustine Langoustine 1 来自天朝的朋友 火狐浏览器 Windows 10 江苏省苏州市 电信

                      看看

                      • 小弟 小弟 0 来自天朝的朋友 谷歌浏览器 Windows 7 湖北省武汉市 联通

                        大哥,我来看看啦

                        • 喃喃道 喃喃道 0 来自天朝的朋友 火狐浏览器 Windows 7 江苏省苏州市 电信

                          学习学习

                          • 大声道 大声道 0 来自天朝的朋友 火狐浏览器 Windows 7 江苏省苏州市 电信

                            学习学习

                            • fast666 fast666 1 来自天朝的朋友 谷歌浏览器 Windows 10 重庆市 联通

                              试试看,好用来打赏

                              • a262831980 a262831980 4 来自天朝的朋友 谷歌浏览器 Windows 7 广东省湛江市 电信

                                感觉不出来啊再

                                • 王小优 王小优 4 来自天朝的朋友 谷歌浏览器 Windows 10 湖南省株洲市 电信

                                  路过学习

                                  • 二爷 二爷 2 来自天朝的朋友 谷歌浏览器 Linux 四川省成都市 移动

                                    看看吧

                                    • 78tnnf 78tnnf 2 来自天朝的朋友 谷歌浏览器 Windows 10 山东省 移动

                                      感谢分享

                                      • 787867 787867 0 来自天朝的朋友 谷歌浏览器 Windows 7 广东省深圳市 联通

                                        感谢分享