WordPress开启sitemap.xml的简单办法

3,155次阅读
一条评论

共计 2163 个字符,预计需要花费 6 分钟才能阅读完成。

最近网站权重掉的很厉害,基本上已经为零了,关键词也全没了,一直在找原因。

可能是最近关闭了许多子域名和子网站导致的吧。

身边的朋友也给出了建议,可能是因为没有sitemap.xml导致的。

才反应过来,自从换成wordpress之后,确实没有搞sitemap.xml,只有主题自带的sitemap.html,对百度收录确实不太友好,写篇文章,权当自我提醒吧。

在WordPress网站根目录新建 sitemap.php 文件,然后粘贴下述代码,保存就可以了。

如果服务器做了伪静态,还需要加入下面这段代码在伪静态里。

Nginx

rewrite ^/sitemap.xml$ /sitemap.php last;

Apache

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

然后访问https://你的域名/sitemap.xml

sitemap.php 代码内容:

<?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>

WordPress开启sitemap.xml的简单办法

沛霖主页 亚洲三帅之首 mp_wx_49e55203ff 哈哈哈哈

共计30人点赞,其中4人来自小程序

正文完
微信扫码打开小程序体验更多功能
post-qrcode
 30
沛霖主页
版权声明:本站原创文章,由 沛霖主页 2023-03-08发表,共计2163字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(一条评论)
World 评论达人 LV.1
2023-03-10 17:49:37 回复

:beer: 可以 学到了

 Macintosh  Chrome  中国广东省深圳市电信