[安全高效]免插件生成完整纯静态wordpress站点地图


对于wordpress新手站长,wordpress网站地图只需要安装Baidu Sitemap Generator插件即可以实现,但是此插件并不支持php7,与最新的程序兼容性也不好,无法自动生成sitemap.xml文件,点击生成是显示空白没有反应的,这就需要我们手动diy,完成一组安全高效的自动站点地图代码.此代码可同时生成首页、文章、分类、页面和标签的sitemap地图结构!

161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566<?phprequire('./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/schemas/sitemap-mobile/1/">'?><!-- generated-on=<?php echo get_lastpostdate('blog'); ?> Diy By 张戈博客(http://zhangge.net)--><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/* 文章页面 */$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); ?>+00:00</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>

将以上代码保存为 sitemap.php,传到网站根目录。手动访问查看效果,如:http://yousite/sitemap.php,上传完成后我们需要对文件进行伪静态操作。
编辑网站根目录的 .htaccess ,加入如下规则:

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

假如是HTTPS站点,需要在wget指令里加上–no-check-certificate的选项的哦,记得前后的空格即可。
通过以上的操作,你会惊喜的发现你的网站网站地图已经将你网站的所有页面的链接地址都汇总在这里了,就等蜘蛛来抓取吧。


上一篇:wordpress增加阅读文章内容时间长度提醒

下一篇:wordpress导入数据库文件出现#1046 – No database selected错误


Copyright © 2002-2019 测速网 www.inhv.cn 皖ICP备2023010105号
测速城市 测速地区 测速街道 网速测试城市 网速测试地区 网速测试街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!

热门搜索 城市网站建设 地区网站制作 街道网页设计 大写数字 热点城市 热点地区 热点街道 热点时间 房贷计算器