WordPress的站点地图在普通的爬虫网总是抓不出来的,所以我们要想办法获取WordPress的sitemap.xml
怎么样实现?代码实现!插件太繁琐了,还是代码好用
以下为源代码
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 ''
?>
daily
1.0
/* 文章页面 */
header("Content-type: text/xml");
$myposts = get_posts( "numberposts=" . $posts_to_show );
foreach( $myposts as $post ) { ?>
monthly
0.6
/* 单页面 */
$mypages = get_pages();
if(count($mypages) > 0) {
foreach($mypages as $page) { ?>
<?php echo get_page_link($page->ID); ?>
<?php echo str_replace(" ","T",get_page($page->ID)->post_modified); ?>+00:00
weekly
0.6
/* 博客分类 */
$terms = get_terms('category', 'orderby=name&hide_empty=0' );
$countcount = count($terms);
if($count > 0){
foreach ($terms as $term) { ?>
<?php echo get_term_link($term, $term->slug); ?>
weekly
0.8
/* 标签(可选) */
$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;
?>
monthly
0.4
复制源码到php文件,然后放到你主机的根目录,然后保存即可
至于使用方法,很简单:直接打开那个页面,然后另存为那个页面的XML文件,在上传到主机即可使用
如果我们想打开该页面就自动刷新XML也不是没办法==办法来了:重写.htaccess,在第一行加上一句代码即可:
RewriteRule ^(sitemap)\.xml$ $1.php
nginx的添加方式
编辑已存在的 Nginx 伪静态规则,新增如下规则后(平滑)重启 nginx 即可:
rewrite ^/sitemap.xml$ /sitemap.php last;