sitemap中的changefreq、priority怎么写?

sitemap中的changefreq、priority怎么写?

资源外星人导读:本站为大家带来sitemap中的changefreq、priority怎么写?文章,更多建站技术,请继续关注资源外星人!

资源外星人曾经有分享过一篇文章《网站地图制作(WordPress免插件)》,最近查看了很多网站的sitemap,对比中发现了一些之前忽略了的问题。下面以我之前的sitemap.xml为例:

<?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="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">'
?>
<!-- generated-on=<?php echo get_lastpostdate('blog'); ?>-->
  <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代码,但因为资源外星人一周会有几次文章更新,其中的一些设置不是特别合适。对上面的代码进行解释:

代码解释
urlset定义.xml的命名空间
url具体某个链接
loc页面永久链接地址
lastmod页面最后修改时间
changefreq页面内容更新频率
priority重要性

下面特别讲解一下changefreq和priority。

changefreq:

是用来告诉搜索引擎网站更新的周期,描述的单词:“always”(经常) 、“hourly”(每时)、“daily”(每天)、“weekly”(每周)、“monthly”(每月)、“yearly”(每年)。像首页就可以用“always”;对于很久前的链接或不再更新内容的链接就可以使用“yearly”。

百度官方明确指出:百度Spider会参考设置周期抓取Sitemap文件,因此请根据Sitemap文件内容的更新(比如增加新url)来设置。请注意若url不变而仅是url对应的页面内容更新(比如论坛帖子页有新回复内容),不在此更新范围内。Sitemap工具不能解决页面更新问题。

priority:用来指定此链接相对于其他链接的优先权比值,取值范围为0.0~1.0之间。值越大,表示此链接的优先权就越高。

百度官方提示:XML格式的 Sitemap 中,“priority”提示会影响我的网页在搜索结果中的排名吗?
不会。Sitemap 中的“priority”提示只是说明该网址相对于您自己网站上其他网址的重要性,并不会影响网页在搜索结果中的排名。

下面是我修改后的代码:

<?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="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">'
?>
  <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>always</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>weekly</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>daily</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中的changefreq、priority怎么写?全部内容,希望对大家有所帮助!