找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 381|回复: 0

wordpress生成sitemap.xml地图

[复制链接]
  • TA的每日心情
    奋斗
    2022-9-1 11:24
  • 签到天数: 50 天

    连续签到: 1 天

    [LV.5]常住居民I

    766680204
    发表于 2020-5-8 18:31:23 | 显示全部楼层 |阅读模式

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×
                    根目录新建sitemap.php
    1. <?php
    2. require('./wp-blog-header.php');
    3. header("Content-type: text/xml");
    4. header('HTTP/1.1 200 OK');
    5. $posts_to_show = 1000;
    6. echo '<?xml version="1.0" encoding="UTF-8"?>';
    7. echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">'
    8. ?>
    9.   <url>
    10.       <loc><?php echo get_home_url(); ?></loc>
    11.       <mobile:mobile type="pc,mobile"/>
    12.       <lastmod><?php $ltime = get_lastpostmodified(GMT);$ltime = gmdate('Y-m-dTH:i:s+00:00', strtotime($ltime)); echo $ltime; ?></lastmod>
    13.       <changefreq>daily</changefreq>
    14.       <priority>1.0</priority>
    15.   </url>
    16. <?php
    17. /* 文章页面 */
    18. $myposts = get_posts( "numberposts=" . $posts_to_show );
    19. foreach( $myposts as $post ) { ?>
    20.   <url>
    21.       <loc><?php the_permalink(); ?></loc>
    22.       <mobile:mobile type="pc,mobile"/>
    23.       <lastmod><?php the_time('c') ?></lastmod>
    24.       <changefreq>monthly</changefreq>
    25.       <priority>0.6</priority>
    26.   </url>
    27. <?php } /* 文章循环结束 */ ?>  
    28. <?php
    29. /* 单页面 */
    30. $mypages = get_pages();
    31. if(count($mypages) > 0) {
    32.     foreach($mypages as $page) { ?>
    33.     <url>
    34.       <loc><?php echo get_page_link($page->ID); ?></loc>
    35.       <mobile:mobile type="pc,mobile"/>
    36.       <lastmod><?php echo str_replace(" ","T",get_page($page->ID)->post_modified); ?>+00:00</lastmod>
    37.       <changefreq>weekly</changefreq>
    38.       <priority>0.6</priority>
    39.   </url>
    40. <?php }} /* 单页面循环结束 */ ?>
    41. <?php
    42. /* 博客分类 */
    43. $terms = get_terms('category', 'orderby=name&hide_empty=0' );
    44. $count = count($terms);
    45. if($count > 0){
    46. foreach ($terms as $term) { ?>
    47.     <url>
    48.       <loc><?php echo get_term_link($term, $term->slug); ?></loc>
    49.       <mobile:mobile type="pc,mobile"/>
    50.       <changefreq>weekly</changefreq>
    51.       <priority>0.8</priority>
    52.   </url>
    53. <?php }} /* 分类循环结束 */?>
    54. <?php
    55. /* 标签(可选) */
    56. $tags = get_terms("post_tag");
    57. foreach ( $tags as $key => $tag ) {
    58.     $link = get_term_link( intval($tag->term_id)] "post_tag" );
    59.          if ( is_wp_error( $link ) )
    60.           return false;
    61.           $tags[ $key ,->link = $link;
    62. ?>
    63. <url>
    64.       <loc><?php echo $link; ?></loc>
    65.       <mobile:mobile type="pc,mobile"/>
    66.       <changefreq>monthly</changefreq>
    67.       <priority>0.4</priority>
    68.   </url>
    69. <?php  } /* 标签循环结束 */ ?>
    70. </urlset>
    复制代码
    伪静态设置
    nginx设置如下伪静态规则:
    1. rewrite ^/sitemap.xml$ /sitemap.php last;
    复制代码


                
    回复

    使用道具 举报

    网站地图|页面地图|文字地图|Archiver|手机版|小黑屋|找资源 |网站地图

    GMT+8, 2024-10-7 12:25

    Powered by Discuz! X3.5

    © 2001-2024 Discuz! Team.

    快速回复 返回顶部 返回列表