WordPress免插件制作网站地图(HTML+XML两个版本)

2018年11月16日13:30:42 发表评论 1,497 views

插件,能少用一个就少用一个,所以陌小雨最近发不了很多免插件的方法,在本主题下都是亲测可行的,希望有你需要的。

Html 网站地图代码如下:

  1. <?php
  2. /**
  3. @package WordPress
  4. Template Name: 站点地图
  5. */
  6. ?>
  7. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  8. <html xmlns="http://www.w3.org/1999/xhtml">
  9. <head profile="http://gmpg.org/xfn/11">
  10. <meta http-equiv="Content-Type" content="text/html; charset=<?php bloginfo( 'charset' ); ?>" />
  11. <title>站点地图 - <?php bloginfo('name'); ?></title>
  12. <meta name="keywords" content="站点地图,<?php bloginfo('name'); ?>" />
  13. <meta name="copyright" content="<?php bloginfo('name'); ?>" />
  14. <link rel="canonical" href="<?php echo get_permalink(); ?>" />
  15. <style type="text/css">
  16. body {font-family: Verdana;FONT-SIZE: 12px;MARGIN: 0;color: #000000;background: #ffffff;}
  17. img {border:0;}
  18. li {margin-top: 8px;}
  19. .page {padding: 4px; border-top: 1px #EEEEEE solid}
  20. .author {background-color:#EEEEFF; padding: 6px; border-top: 1px #ddddee solid}
  21. #nav, #content, #footer {padding: 8px; border: 1px solid #EEEEEE; clear: both; width: 95%; margin: auto; margin-top: 10px;}
  22. </style>
  23. </head>
  24. <body vlink="#333333" link="#333333">
  25. <h2 style="text-align: center; margin-top: 20px"><?php bloginfo('name'); ?>'s SiteMap </h2>
  26. <center></center>
  27. <div id="nav"><a href="<?php bloginfo('url'); ?>/"><strong><?php bloginfo('name'); ?></strong></a> &raquo; <a href="<?php echo get_permalink(); ?>">站点地图</a></div>
  28. <div id="content">
  29. <h3>最新文章</h3>
  30. <ul>
  31. <?php
  32. $previous_year = $year = 0;
  33. $previous_month = $month = 0;
  34. $ul_open = false;
  35. $myposts = get_posts('numberposts=-1&orderby=post_date&order=DESC');
  36. foreach($myposts as $post) :
  37. ?>
  38. <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li>
  39. <?php endforeach; ?>
  40. </ul>
  41. </div>
  42. <div id="content">
  43. <li class="categories">分类目录<ul>
  44. <?php wp_list_categories('title_li='); ?>
  45. </ul></li>
  46. </div>
  47. <div id="content">
  48. <li class="categories">单页面</li>
  49. <?php wp_page_menu(); ?>
  50. </div>
  51. <div id="footer">查看博客首页: <strong><a href="<?php bloginfo('url'); ?>/"><?php bloginfo('name'); ?></a></strong></div><br />
  52. <center>
  53. <div style="text-algin: center; font-size: 11px">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>
  54. </center>
  55. <center>
  56. <div style="text-algin: center; font-size: 11px">Powered by <strong><a href="https://dedewp.com/" target="_blank">免插件 SiteMap</a></strong>&nbsp;
  57. &copy; <?php echo date('Y'); ?> <a href="<?php bloginfo('url');?>/" style="cursor:help"><?php bloginfo('name');?></a> 版权所有.<br /><br /></div>
  58. </center>
  59. </body>
  60. </html>

代码使用方法:新建一个记事本,重命名为 sitemap.php,复制上面的代码到里面,保存,上传到当前主题目录下,然后在后台新建页面,模版选择站点地图,在外观-菜单中将该页面加入主导航就行了。

2016 年 3 月 31 日更新:

HTML 格式的网站地图还有一种写法,也分享给大家:

  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(); ?>
  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="https://dedewp.com/sitemap.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>

自己选择喜欢的一种吧。

如果需要制作 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. /* 博客分类 */
  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 文件第一行添加如下跳转代码:

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

在需要显示的地方添加超级链接就可以了

  1. https://38hack.com/sitemap.xml

将上述域名换为你自己的。具体效果见本站底部 html 网站地图和 XML 网站地图。

站长小智

发表评论

您必须登录才能发表评论!