HMJ-Blog主题优化:无插件实现html和xml地图

自媒体 思享 1826浏览

我们知道网站地图能够很好的将整站的结果展现给搜索引擎,更利于搜索引擎抓取网页的内容,之前博客一直用得

Baidu Sitemap Generator插件实现的。如今在网上找到了一个可以不用插件的方法,现在分享给大家

生成html地图

生成html地图的方法比较简单,就是通过一个文件模板生成一个地图单页面就可以了

1、下载sitemap.php文件,通过ftp上传到主题文件夹里。

下载地址:http://pan.baidu.com/s/1c1GlCHi

2、新建一个单页面,注意这个单页面的地址就是网站地图的地址

例如:我的网站地图地址是:https://www.4xseo.com/sitemap/

html地图制作

地图生成以后,可以放在底部或顶部,方便用户浏览,也可以在robots.txt中添加

  1. Sitemap: https://www.4xseo.com/sitemap

生成xml地图

html地图其实更多是给用户看的,而xml就完全是给搜索引擎“看”的了,生成xml的步骤稍微复杂些,需要设置url转发

具体步骤如下

1、下载xmlmap.php文件,通过ftp上传到网站根目录下。

下载地址:http://pan.baidu.com/s/1mh7NMn2

2、创建一个sitemap.xml文件,通过ftp上传到网站根目录下。

3、设置url转发规则文件:

如果你的主机是Apache,在.htaccess添加以下重写规则:

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

注意要放在,# BEGIN WordPress和# END WordPress 之外,否则有可能被自动删除

如果你的主机是nginx,在.htaccess添加以下重写规则:

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

如果你的主机是IIS,在web.config添加以下重写规则:

  1. <rule name="Rewrite to xmlmap.php" stopProcessing="true">
  2. <match url="^sitemap.xml" />
  3. <action type="Rewrite" url="xmlmap.php" />
  4. </rule>

同样,可以在robots.txt中添加如下说明,告知搜索引擎

  1. Sitemap: https://www.4xseo.com/sitemap.xml

20161216更新

以上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="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'); ?> Diy By 余斗-->  
  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); ?></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. $count = 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>  

推荐阅读

WordPress4.9分支最新版更新至4.9.25

WordPress其实很早之前就已经更新到6.x版本了,不过本博客一直使用4.9分支。幸运的是,这个版本的分支仍在更新中。该版本已于2024年1月31日更新至4.9.25版本。 下载链接 [下载]https://cn.wordpress.......

纯代码实现wordpress附件页面重定向到文章或首页

前几天发现wordpress网站有评论留言的回顾。结果一看就是附件页面的垃圾评论。这才发现原来wordpress上传的附件也会有对应的页面。难怪之前收录了很多附件页面,但是我的机器人禁止了,忘记了。本来以为会禁用,结果只找到了在线使用插件的方法。最后......

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘fake_update_callback’

这是当一个被挂钩的函数名与挂钩关联不匹配时…这可能发生在重命名一个函数时,而不是在挂钩关联中重命名函数名时。如果强迫症受不了wordpress的主题提示,可以使用插件WP降级将版本设置为当前版本。...