首页 Wordpress WordPress 生成sitemap.xml的另一种方法

WordPress 生成sitemap.xml的另一种方法

之前说过,Wordpress的网站地图sitemap可以通过function.php里的一段代码来生成《WordPress 自动生成网站地图sitemap.xml》,今天再分享一个方法,就是通过访问一个PHP文件自动生成.xml文件。

一、建立一个PHP文件

比如set-sitamap.php,把下面的代码扔进去
php
<?php
// 自动加载WordPress
$wp_load = __DIR__ . '/wp-load.php';
if (!file_exists($wp_load)) $wp_load = __DIR__ . '/../wp-load.php';
require_once($wp_load);

// 关闭所有PHP警告输出(避免污染XML/JSON)
error_reporting(0);
@ini_set('display_errors', 0);

// 关闭WordPress自带sitemap
add_filter('wp_sitemaps_enabled', '__return_false');

// 生成sitemap函数
function build_sitemap() {
    $savePath = ABSPATH . 'sitemap.xml';
    $xml = '<?xml version="1.0" encoding="UTF-8"?>';
    $xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.baidu.com/schemas/sitemap-mobile/1/">';
    // 首页
    $homeUrl = htmlspecialchars(str_replace('http://', 'https://', get_home_url()));
    $xml .= '<url>';
    $xml .= '<loc>' . $homeUrl . '</loc>';
    $xml .= '<lastmod>' . gmdate('Y-m-d\TH:i:s+00:00') . '</lastmod>';
    $xml .= '<changefreq>daily</changefreq>';
    $xml .= '<priority>1.0</priority>';
    $xml .= '</url>';
    // 文章(增加空值判断)
    $posts = get_posts(['numberposts' => 9999, 'post_status' => 'publish', 'post_type' => 'post']);
    if (!empty($posts)) {
        foreach ($posts as $post) {
            $permalink = htmlspecialchars(str_replace('http://', 'https://', get_permalink($post->ID)));
            $lastmod = gmdate('Y-m-d\TH:i:s+00:00', strtotime($post->post_modified_gmt));
            $xml .= '<url>';
            $xml .= '<loc>' . $permalink . '</loc>';
            $xml .= '<lastmod>' . $lastmod . '</lastmod>';
            $xml .= '<changefreq>monthly</changefreq>';
            $xml .= '<priority>0.6</priority>';
            $xml .= '</url>';
        }
    }
    // 页面(增加空值判断)
    $pages = get_pages(['post_status' => 'publish']);
    if (!empty($pages)) {
        foreach ($pages as $page) {
            $pageLink = htmlspecialchars(str_replace('http://', 'https://', get_page_link($page->ID)));
            $lastmod = gmdate('Y-m-d\TH:i:s+00:00', strtotime($page->post_modified_gmt));
            $xml .= '<url>';
            $xml .= '<loc>' . $pageLink . '</loc>';
            $xml .= '<lastmod>' . $lastmod . '</lastmod>';
            $xml .= '<changefreq>weekly</changefreq>';
            $xml .= '<priority>0.6</priority>';
            $xml .= '</url>';
        }
    }
    // 分类(判断+过滤)
    $categories = get_terms(['taxonomy' => 'category', 'hide_empty' => true, 'fields' => 'all']);
    if (!is_wp_error($categories) && !empty($categories)) {
        foreach ($categories as $cat) {
            $catLink = htmlspecialchars(str_replace('http://', 'https://', get_term_link($cat)));
            $xml .= '<url>';
            $xml .= '<loc>' . $catLink . '</loc>';
            $xml .= '<lastmod>' . gmdate('Y-m-d\TH:i:s+00:00') . '</lastmod>';
            $xml .= '<changefreq>weekly</changefreq>';
            $xml .= '<priority>0.3</priority>';
            $xml .= '</url>';
        }
    }
    // 标签(已修复)
    $tags = get_terms(['taxonomy' => 'post_tag', 'hide_empty' => true]);
    if (!is_wp_error($tags) && !empty($tags)) {
        foreach ($tags as $tag) {
            $link = get_term_link($tag);
            if (is_wp_error($link)) continue;
            $tagLink = htmlspecialchars(str_replace('http://', 'https://', $link));
            $xml .= '<url>';
            $xml .= '<loc>' . $tagLink . '</loc>';
            $xml .= '<lastmod>' . gmdate('Y-m-d\TH:i:s+00:00') . '</lastmod>';
            $xml .= '<changefreq>monthly</changefreq>';
            $xml .= '<priority>0.3</priority>';
            $xml .= '</url>';
        }
    }
    $xml .= '</urlset>';
    file_put_contents($savePath, $xml);
}
// 手动生成:访问本文件即可生成
if (!isset($_GET['auto'])) {
    build_sitemap();
    echo "✅ sitemap 生成完成!
访问地址:<a href='".home_url('/sitemap.xml')."' target='_blank'>".home_url('/sitemap.xml')."</a>";
    exit;
}

// 自动生成:发布/更新文章时自动刷新
add_action('save_post', function() {
    build_sitemap();
});
把代码放到网站根目录,就是与wp-config.php同级目录,这个很重要,不是在主题目录

二、访问这个PHP文件

访问这个PHP文件,就可以自动生成sitemap.xml文件了,比如我的:https://wuean.com/set-sitemap.php,就会出现下面的样子,OK,搞定! 就可以访问网站的sitemap.xml文件了

相关推荐

发表回复

邮箱地址不会被公开。