WordPress 自动生成网站地图sitemap

Gavin Wu 2022年03月19日 8:03 Wordpress 1,423 Views

最近又折腾起wordpress ,重新搞sitemap.xml网站地图文件,插件非常多就不说了,不用插件的方法网上也有很多相关的代码,其中张戈博客的最具代表性,但多次尝试后出现错误,而且需要伪静态设置,特别麻烦。
在CSDN中找到一个办法比较简单,适合我们这种小白操作。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
    include ( "wp-config.php" ) ;
    require_once (ABSPATH.'./wp-blog-header.php');
    $ltime = get_lastpostmodified(GMT);
    $ltime = gmdate('Y-m-d\TH:i:s+00:00', strtotime($ltime));
    $posts_to_show = 2000;
    $str = '<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">';
        $str.="
        <url>
        <loc>"
.get_home_url()."</loc>
        <lastmod>"
.$ltime."</lastmod>
        <changefreq>weekly</changefreq>
        <priority>1.0</priority>
        </url>
        "
;

    //文章 code by 3inch.cn
    $myposts = get_posts( "numberposts=".$posts_to_show );
    foreach( $myposts as $post ) {
        $str.="<url>\r\n";
        $str.="<loc>".urldecode(get_permalink())."</loc>\r\n";
        $str.="<lastmod>".str_replace(" ","T",get_page($page->ID)->post_modified)."</lastmod>\r\n";
        $str.="<changefreq>always</changefreq>\r\n";
        $str.="<priority>0.9</priority>\r\n";
        $str.="</url>\r\n";
    }
    //标签 code by 3inch.cn
    $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;

        $str.="<url>\r\n";
        $str.="<loc>".urldecode($link)."</loc>\r\n";
        $str.="<lastmod>".str_replace(" ","T",get_page($page->ID)->post_modified)."</lastmod>\r\n";
        $str.="<changefreq>daily</changefreq>\r\n";
        $str.="<priority>0.5</priority>\r\n";
        $str.="</url>\r\n";
    }
    //分类 code by 3inch.cn
    $terms = get_terms('category', 'orderby=name&hide_empty=0' );
    $count = count($terms);
    if($count > 0){
        foreach ($terms as $term) {
            $str.="<url>\r\n";
            $str.="<loc>".urldecode(get_term_link($term, $term->slug))."</loc>\r\n";
            $str.="<lastmod>".str_replace(" ","T",get_page($page->ID)->post_modified)."</lastmod>\r\n";
            $str.="<changefreq>weekly</changefreq>\r\n";
            $str.="<priority>0.3</priority>\r\n";
            $str.="</url>\r\n";
            }
    }
    //页面 code by 3inch.cn
    $mypages = get_pages();
    if(count($mypages) > 0) {
        foreach($mypages as $page) {
        $str.="<url>\r\n";
        $str.="<loc>".urldecode(get_page_link($page->ID))."</loc>\r\n";
        $str.="<lastmod>".str_replace(" ","T",get_page($page->ID)->post_modified)."</lastmod>\r\n";
        $str.="<changefreq>monthly</changefreq>\r\n";
        $str.="<priority>0.5</priority>\r\n";
        $str.="</url>\r\n";
      }
    }

    $str.="</urlset>";
    file_put_contents('./sitemap.xml',$str);
    echo '更新 sitemap.xml 成功! <a href="/sitemap.xml"> 查看</a>';
?>
使用方法

1、新建sitemap.php文件,将下述代码贴到里面。
2、将sitemap.php文件上传到网站根目录里
3、访问上传的文件,http://XXX.XX/sitemap.php,会自动更新xml文件。
4、可以提交搜索引擎了。

WordPress 自动生成网站地图sitemap|微言心语
WordPress 自动生成网站地图sitemap|微言心语
本站演示:https://wuean.com/sitemap.xml

sitemap.php下载

提取码: 7ja1

发表回复

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

Top