百度收录wordpress之sitemap推送

用以下代码同时生成三个文件站点地图
sitemap.xml 谷歌,必应,雅虎
sitemap.html 百度
sitemap.txt 其它

<?php
include ("wp-config.php");
require_once (ABSPATH.'wp-blog-header.php');
//header("Content - type: text / xml");
header('Content-type: application/xml; charset="utf-8"');
header('HTTP/1.1 200 OK');
home_url();
$posts_to_show = 1000;
$mainhost      = get_home_url();
$newLine       = "\n";
$urlsetOpen    = "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">$newLine";
$xmlstr        = '<?xml version="1.0" encoding="UTF-8"?>'.$newLine;
$xmlstr .= $urlsetOpen.$newLine;
$txtstr = get_home_url().$newLine;
$html   = '<!DOCTYPE html>
<head>
<title>'.get_bloginfo('name').'-站点地图 sitemap</title>
<meta http-equiv="Content-type" content="text/html;" charset="UTF-8" />
<style>
body{font-family: Arial, "微软雅黑";font-size: 13px;}
ul, li{margin:0px; padding:0px; list-style:none;}ul{width:800px;margin-left: auto;margin-right: auto;}.title{width:800px;font-size: 18px;}.lks{float: left;padding-right: 15px;line-height: 30px;font-size: 16px;}
</style>
</head><body align="center">
<ul><li class="title"><h3>'.get_home_url().'站点地图 sitemap:('.date('Y-m-d').')</h3></li>
';
$ltime  = get_lastpostmodified('gmt');
$ltime  = gmdate('Y-m-d\TH:i:s+00:00', strtotime($ltime));
$xmlstr .= "<url>
<loc>".get_home_url()."</loc>
<lastmod>".$ltime."</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
<data>
<display>
<title>time machine</title>
<content>开源趣味软件收录分享,编程手册,精美代码收藏。</content>
<tag>时光机,开源趣味软件</tag>
<pubTime>".$ltime."</pubTime>
<thumbnail loc=\"".get_home_url()."/cover.jpg\"/>
</display>
</data>
</url>
";
/* 博客分类 */
$terms = get_terms('category', 'orderby=name&hide_empty=0');
$count = count($terms);
if ($count > 0)
{
  foreach ($terms as $term)
  {
    $xmlstr .= "<url>$newLine";
    $xmlstr .= "<loc>".get_term_link($term, $term->slug)."</loc>$newLine";
    //$xmlstr .= " < lastmod></lastmod > $newLine";
    $xmlstr .= "<changefreq>weekly</changefreq>$newLine";
    $xmlstr .= "<priority>0.6</priority>$newLine";
    $xmlstr .= "</url>$newLine";
    $html .= '<li class="lks"><a href="'.get_term_link($term, $term->slug).'" title="'.$term->name.'" target="_blank">'.$term->name.'</a></li>'.$newLine;
    $txtstr .= get_term_link($term, $term->slug).$newLine;
  }
}
$tags = get_terms("post_tag");
$count= count($tags);
if ($count > 0)
{
  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;
    $xmlstr .= "<url>$newLine";
    $xmlstr .= "<loc>".$link."</loc>$newLine";
    //$xmlstr .= " < lastmod></lastmod > $newLine";
    $xmlstr .= "<changefreq>monthly</changefreq>$newLine";
    $xmlstr .= "<priority>0.4</priority>$newLine";
    $xmlstr .= "</url>$newLine";
    $html .= '<li class="lks"><a href="'.$link.'" title="'.$tag->name.'" target="_blank">'.$tag->name.'</a></li>'.$newLine;
    $txtstr .= $link.$newLine;
  }
}
/* 单页面 */
$mypages = get_pages();
if (count($mypages) > 0)
{
  foreach ($mypages as $page)
  {
    $xmlstr .= "<url>$newLine";
    $xmlstr .= "<loc>".get_page_link($page->ID)."</loc>$newLine";
    $xmlstr .= "<lastmod>".str_replace(" ","T",get_page($page->ID)->post_modified)."+00:00</lastmod>$newLine";
    $xmlstr .= "<changefreq>weekly</changefreq>$newLine";
    $xmlstr .= "<priority>0.6</priority>$newLine";
    $xmlstr .= "</url>$newLine";
    $html .= '<li class="lks"><a href="'.get_page_link($page->ID).'" title="'.$page->post_title.'" target="_blank">'.$page->post_title.'</a></li>'.$newLine;
    $txtstr .= get_page_link($page->ID).$newLine;
  }
}
/* 文章页面 */
$myposts = get_posts("numberposts=" . $posts_to_show); //get_posts();
foreach ($myposts as $post)
{
  $xmlstr .= "<url>$newLine";
  $xmlstr .= "<loc>".urldecode(get_permalink())."</loc>$newLine";
  //$xmlstr .= " < loc > ".urldecode(the_permalink())."</loc > $newLine";
  $xmlstr .= "<lastmod>".get_the_time('c')."</lastmod>$newLine";
  $xmlstr .= "<changefreq>monthly</changefreq>$newLine";
  $xmlstr .= "<priority>0.8</priority>$newLine";
  $xmlstr .= "</url>$newLine";
  $html .= '<li class="lks"><a href="'.get_permalink().'" title="'.$post->post_title.'" target="_blank">'.$post->post_title.'</a></li>'.$newLine;
  $txtstr .= get_permalink().$newLine;
}
$xmlstr .= "</urlset>";
$html .= '</ul></body></html>';
file_put_contents('./sitemap.xml',$xmlstr);
file_put_contents('./sitemap.html',$html);
file_put_contents('./sitemap.txt',$txtstr);
echo $xmlstr;
?>

链接提交地址:http://zhanzhang.baidu.com/linksubmit/url

以上三种把文章的新链接推送到百度,实现网站新页面快速收录的方法介绍就完成了,第二天就可以在后台看到每天推送给百度的链接数量。

百度站站管理工具:http://zhanzhang.baidu.com/dashboard/index

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注