前言:网站地图为何还分Html和Xml两个版本?
在有客看来,一个html网站地图不管是对搜索引擎还是访问的用户来说都比较友好,而单纯一个xml格式的网站地图只有搜索引擎才能看得懂了。
虽然说如今网站地图的重要性大不如前,但总比好过什么都不做吧?今天有客就来分享下在不使用插件的情况下,该如何使用代码来为WordPress生成不同格式的网站地图。
使用代码为WordPress生成HTML网站地图
有客之前就说过,html格式的网站地图不管是对用户还是对搜索引擎都要更加友好,那么有客就来说说html网站地图的实现方法。
为了让大家明白什么是HTML网站地图,有客就先贴一张截图让大家能够更直观的了解好了。
有客网站地图截图
上面就是有客HTML网站地图的截图了。是不是觉得比XML格式的地图更加直观呢?
HTML网站地图实现代码
其实HTML网站地图实现起来非常简单,请复制下面有客贴出的代码,然后保存为sitemap.php文件,保存好之后就上传到你当前WordPress主题的根目录即可。
<?php /* Template Name: 站点地图 */ ?> <!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'); ?>'s 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">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> </center> </body> </html>
HTML站点地图调用方式
将上面有客贴出的代码保存为sitemap.php并且上传到你WordPress主题的目录下之后,请按照有客下面给出的步骤进行操作。
- 第一步:打开后台→页面→新建页面
- 第二步:标题处输入网站地图,链接处输入sitemap,页面模版处选择“站点地图”
- 第三步:点击发布按钮发布这个设置好的页面。
完成上面三个步骤之后,你就可以直接去前台看看效果了,
XML格式的网站地图生成代码
说完了HTML格式的网站地图实现方式,有客就接着说下XML格式的网站地图该如何生成好了。
实现方法很简单,你只需要先复制下面有客贴出的代码,燃后保存为xmlmap.php文件上传到网站根目录即可(注意是网站根目录不是WordPress主题目录),
<?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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'; ?> <!-- generated-on=<?php echo get_lastpostdate('blog'); ?>--> <url> <loc>http://www.v7v3.com/</loc> <lastmod><?php echo get_lastpostdate('blog'); ?></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 } // end foreach ?> </urlset>
设置XML格式地图的转发规则
虽然说访问xmlmap.php也一样可以查看,但我们完全可以设置转发规则将后缀php改成XML。
RewriteEngine On RewriteBase / RewriteRule ^sitemap.xml$ xmlmap.php
Nginx转发规则
rewrite ^/sitemap.xml$ /xmlmap.php;
最后说明下
如果你不喜欢网站地图的链接形式,你可以根据自己的喜好通过转发规则进行修改,比如将html格式的网站地图改成 sitemap.html 的形式,但这其实并没有什么用,因为搜索引擎都能够完美识别。
转载请注明:有客帮 » 纯代码实现WordPress生成HTML网站地图和XML网站地图