一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

wordpress在重写sitemap提交给百度的例子

时间:2015-12-11 编辑:简简单单 来源:一聚教程网

 

在之前我的是sitemap的一个插件,用起来非常的不爽,每次都要手动去生成sitemap,以我每天更新文章的频率每天都要点一下,感觉太麻烦了,不点的话百度站长平台里的sitemap就不会更新

 

1.jpg

 

这不影响收录吗,于是想这个重写一个sitemap.php,然后伪静态成sitemap.xml,这种方式之前做电子商务网站的时候就这么搞的,后来可能对百度蜘蛛不太友好想了想换了种方式,用定时脚步定时生成sitemap.xml
但是一直都没有这么做,心里默念凑合用用于是就这么过来了,就在升级到php7之后,发现插件有问题,点击生成sitemap的时候空白页,应该是有函数不支持或者没有启用,那就直接重写个sitemap

下面我把其中的代码分享一下

 代码如下复制代码
 require_once( dirname( __FILE__ ) . '/wp-load.php' );
 
$lc_blog_url = home_url();
$blogtime = current_time('timestamp', Ƈ');
$lc_blog_time = date("Y-m-d\TH:i:s+00:00",$blogtime);
$befor_day=date("Y-m-d H:i:s",strtotime("-1 day"));
 
$site_xml='                                    
xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">'
 
$site_xml.= "$lc_blog_url$lc_blog_timedaily1.0";
 
$sql_mini = "select ID,post_modified,post_date,post_type,post_title FROM $wpdb->posts
         WHERE post_password = ''
   AND (post_type='post' or post_type='page')
   AND post_status = 'publish'
   ORDER BY post_modified DESC
        ";
 $recentposts_mini = $wpdb->get_results($sql_mini);
 if($recentposts_mini){
  foreach ($recentposts_mini as $post){
   
   if( $post->post_type == 'page' ){
    $loc = get_page_link($post->ID);
    $loc=sitemap_xml($loc);
    if(!$loc){ continue; }
    if($post->post_modified == 񟍰-00-00 00:00:00'){ $post_date = $post->post_date; } else { $post_date = $post->post_modified; }
    $lastmod = date("Y-m-d\TH:i:s+00:00",strtotime($post_date));
    $changefreq = 'weekly'
    $priority = Ɔ.3'
    $xml_contents_page .= "";
    $xml_contents_page .= "$loc";
    $xml_contents_page .= ''
    $xml_contents_page .= "$lastmod";
    $xml_contents_page .= "$changefreq";
    $xml_contents_page .= "$priority";
    $xml_contents_page .= "
";
    $html_contents .= '
  • '.$post->post_title.'

  • '


  •    }else{


  •     $loc = get_permalink($post->ID);


  •     


  •     $loc = sitemap_xml($loc);


  •     if(!$loc){ continue; }


  •     if($post->post_modified == 񟍰-00-00 00:00:00'){ $post_date = $post->post_date; } else { $post_date = $post->post_modified; } ##$post->post_date_gmt


  •     $lastmod = date("Y-m-d\TH:i:s+00:00",strtotime($post_date));


  •     $changefreq = 'monthly'


  •     $priority = Ɔ.6'


  •     $xml_contents_post .= "

  • ";
        $xml_contents_post .= "$loc";
        $xml_contents_post .= ''
        $xml_contents_post .= "$lastmod";
        $xml_contents_post .= "$changefreq";
        $xml_contents_post .= "$priority";
        $xml_contents_post .= "
  • ";


  •  


  •     $html_contents .= '

  • '.$post->post_title.'

  • '


  •     


  •     if($post->post_date>$befor_day || $post->post_modified>$befor_day){


  •      


  •      $tags = wp_get_post_tags($post->ID);


  •      foreach($tags as $this_tag){


  •       $loc = get_tag_link($this_tag->term_id);


  •       $loc = sitemap_xml($loc);


  •       


  •  


  •       if(!$loc){ continue; }


  •       $lastmod = date("Y-m-d\TH:i:s+00:00",current_time('timestamp', Ƈ'));


  •       $changefreq = 'Weekly'


  •       $priority = Ɔ.3'      $xml_contents_post .= "

  • ";
          $xml_contents_post .= "$loc";
          $xml_contents_post .= ''
          $xml_contents_post .= "$lastmod";
          $xml_contents_post .= "$changefreq";
          $xml_contents_post .= "$priority";
          $xml_contents_post .= "
  • ";


  •       $not_tags[$tags->term_id]=1;


  •      }


  •     }


  •    


  •    }


  •   


  •   }


  •  }


  • #栏目


  •  $category_ids = get_all_category_ids();


  •  if($category_ids){


  •   foreach($category_ids as $cat_id) {


  •    $loc = get_category_link($cat_id);


  •    $loc = sitemap_xml($loc);


  •    if(!$loc){ continue; }


  •    $lastmod = date("Y-m-d\TH:i:s+00:00",current_time('timestamp', Ƈ'));


  •    $changefreq = 'Weekly'


  •    $priority = Ɔ.3'


  •    $xml_contents_cat .= "

  • ";
       $xml_contents_cat .= "$loc";
       $xml_contents_cat .= ''
       $xml_contents_cat .= "$lastmod";
       $xml_contents_cat .= "$changefreq";
       $xml_contents_cat .= "$priority";
       $xml_contents_cat .= "
  • ";


  •   }


  •  }


  • #tag


  •  


  •  $all_the_tags = get_tags(array('orderby'=>'term_id','order'=>'desc'));


  •  if($all_the_tags){


  •   foreach($all_the_tags as $this_tag) {


  •    


  •    $tag_id = $this_tag->term_id;


  •    if($not_tags[$tag_id]){


  •     continue;


  •    }


  •    


  •    $loc = get_tag_link($tag_id);


  •    $loc = sitemap_xml($loc);


  •    if(!$loc){ continue; }


  •    $lastmod = date("Y-m-d\TH:i:s+00:00",current_time('timestamp', Ƈ'));


  •    $changefreq = 'Weekly'


  •    $priority = Ɔ.3'


  •    $xml_contents_tag .= "

  • ";
       $xml_contents_tag .= "$loc";
       $xml_contents_tag .= ''
       $xml_contents_tag .= "$lastmod";
       $xml_contents_tag .= "$changefreq";
       $xml_contents_tag .= "
    $priority";
       $xml_contents_tag .= "
  • ";


  •   }


  •  }


  • $xml_contents = $xml_contents_post.$xml_contents_page.$xml_contents_cat.$xml_contents_tag;


  • $site_xml= $site_xml.$xml_contents.'

  • '


  • file_put_contents("sitemap.xml",$site_xml);


  •  


  • function sitemap_xml($string) {


  •  return str_replace ( array ( '&', '"', "'", '<', '>'), array ( '&' , '"', ''' , '<' , '>'), $string);


  • }


把上面的代码保持到根目录下sitemap.php里,然后用定时脚步执行sitemap.php
wordpress又少了个插件,对wordpress的速度可能有所提升何乐而不为

热门栏目