网站地图,一般放置在网站的底部。又分为XML地图和HTML地图,XML主要是方便搜索引擎的收录。HTML是给人看的。
网站地图生成可以使用插件来实现,也可以使用纯代码。新手站长可以使用插件来直接生成,简单方便快捷。如果不想用插件,觉得会拖慢网站速度,可以使用纯代码来实现。
XML地图生成
XML地图主要面向搜索引擎蜘蛛,用来加快网站收录。无梦这里将插件和代码都讲一下
插件生成XML地图
用来生成XML地图的插件有很多,无梦自己的网站用的是Google XML Sitemaps,什么都不用管,只要安装就行了。
安装完成后,不用配置,直接打开你的网站域名/sitemap.xml就可以看到了。
纯代码生成XML地图
插件会拖慢运行速度,大家都是弄网站的,都喜欢自己动手,很有成就感。大部分站长都喜欢纯代码实现功能。
新建PHP文件xmlmap.php,将如下代码复制进去,上传的网站根目录。然后打开你的网站域名/xmlmap.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'); ?> Diy By 余斗--> <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); ?></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需要实现伪静态:
Apache主机,在.htaccess文件中添加以下重写规则:
RewriteEngine On RewriteBase / RewriteRule ^sitemap.xml$ xmlmap.php
nginx主机,在.htaccess文件中添加以下重写规则:
rewrite ^/sitemap.xml$ /xmlmap.php;
IIS主机,在web.config文件中添加以下重写规则:
<rule name="Rewrite to xmlmap.php"stopProcessing="true"> <match url="^sitemap.xml" /> <action type="Rewrite" url="xmlmap.php" /> </rule>
HTML地图生成
html地图的生成也有插件版和纯代码。不过插件很久没更新,不少站长已经弃用了。而且代码生成很简单,无梦这里只讲纯代码生成。
和XML一样,新建PHP文件sitemap.php,复制下方代码进去,上传到主题目录,注意不要上传到网站根目录。
<?php /* Template Name: Sitemap */ ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head profile="http://gmpg.org/xfn/11"> <meta http-equiv="Content-Type"content="text/html; charset=<?php bloginfo( 'charset' ); ?>" /> <title>站点地图_<?php bloginfo('name'); ?></title> <meta name="keywords" content="站点地图,<?php bloginfo('name'); ?>" /> <meta name="copyright" content="<?php bloginfo('name'); ?>" /> <link rel="canonical" href="<?php echo get_permalink(); ?>" /> <style type="text/css"> body {font-family: Microsoft Yahei,Verdana;font-size:13px;margin:0 auto;color: #000000;background: #ffffff;width: 990px;margin: 0 auto} a:link,a:visited {color:#000;text-decoration:none;} a:hover {color:#08d;text-decoration:none;} h1,h2,h3,h4,h5,h6 {font-weight:normal;} img {border:0;} li {margin-top: 8px;} .page {padding: 4px; border-top: 1px #EEEEEE solid} .author {background-color:#EEEEFF; padding: 6px; border-top: 1px #ddddee solid} #nav, #content, #footer {padding: 8px; border: 1px solid #EEEEEE; clear: both; width: 95%; margin: auto; margin-top: 10px;} </style> </head> <body vlink="#333333" link="#333333"> <h2 style="text-align: center; margin-top: 20px"><?php bloginfo('name'); ?> SiteMap </h2> <center></center> <div id="nav"><a href="<?php bloginfo('url'); ?>/"><strong><?php bloginfo('name'); ?></strong></a> » <a href="<?php echo get_permalink(); ?>">站点地图</a></div> <div id="content"> <h3>最新文章</h3> <ul> <?php $previous_year = $year = 0; $previous_month = $month = 0; $ul_open = false; $myposts = get_posts('numberposts=-1&orderby=post_date&order=DESC'); foreach($myposts as $post) : ?> <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> </div> <div id="content"> <li class="categories">分类目录<ul> <?php wp_list_categories('title_li='); ?> </ul></li> </div> <div id="content"> <li class="categories">单页面</li> <?php wp_page_menu( $args ); ?> </div> <div id="footer">查看博客首页: <strong><a href="<?php bloginfo('url'); ?>/"><?php bloginfo('name'); ?></a></strong></div><br /> <center> <div style="text-algin: center; font-size: 11px"><strong>SiteMap</strong> 上次更新: <?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', strtotime($last[0]->MAX_m));echo $last; ?><br /><br /></div> </center> </body> </html>
然后打开WordPress后台,新建页面,网站模板选择Sitemap,更改链接为网站域名/sitemap/。
无梦个人建议大家使用插件来生成XML地图,使用纯代码来生成HTML。XML代码版需要手动添加伪静态重写规则,部分站长可能会抓瞎。
至此,网站地图就全部生成了,各位可以将XML地图提交至搜索引擎,也可以将HTML地图都放置到网站底部供游客观看。
暂无评论内容