没有所谓的捷径
一切都是时间最平凡的累积

wordpress网站地图sitemap生成处理办法

本文最后更新:2019年8月19日,已超过1705天未更新,如果文章内容失效,请留言反馈本站。

第一步:复制以下这段代码,保存为sitemap.php,上传到网站根目录。

<?php
require('./wp-load.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>daily</changefreq>
      <priority>1.0</priority>
  </url>
<?php
/* 文章页面 */ 
header("Content-type: text/xml");
$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>

第二步:在.htaccess里面添加跳转规则。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^sitemap.xml$ sitemap.php
</IfModule>

提醒:在后台设置时wordpress会自动生成替换htaccess里面的规则,所以这段跳转规则单独添加,不建议放到原本自动生成的代码中,否则不知道什么时候设置过就被替换掉了。

nginx的服务在站点配置文件中添加

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

并重启nginx服务。

最后访问https://blog.mydns.vip/sitemap.xml 试下效果吧

» 站长码字辛苦,有用点个赞吧,也可以打个
» 若转载请保留本文转自:豫章小站 » 《wordpress网站地图sitemap生成处理办法》
» 本文链接地址:https://blog.mydns.vip/405.html
» 如果喜欢可以: 点此订阅本站 有需要帮助,可以联系小站
赞(1) 打赏 【豫章小站原创文章】
声明:本站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,若涉及侵权请及时告知,将会在第一时间删除,联系邮箱:contact@mydns.vip。文章观点不代表本站立场。本站原创内容未经允许不得转载,或转载时需注明出处:豫章小站 » wordpress网站地图sitemap生成处理办法
分享到: 更多 (0)

评论 4


  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #2

    I’m not sure where you are getting your info,
    but great topic. I needs to spend some time learning much more or understanding more.
    Thanks for wonderful info I was looking for this information for my mission.

    Dewa Poker5年前 (2019-03-20) 19:13回复
  2. #1

    It is not my first time to pay a quick visit this web site, i am visiting this website dailly and get nice information from here daily.

    Poker5年前 (2019-03-11) 03:45回复

智慧源于勤奋,伟大出自平凡

没有所谓的捷径,一切都是时间最平凡的累积,今天所做的努力都是在为明天积蓄力量

联系我们赞助我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏