在 WordPress 5.5 中,引入了一项新功能XML Sitemaps Functionality,它将基本的、可扩展的XML 站点地图功能添加到 WordPress核心。
在 5.5 版中,WordPress 将在/wp-sitemap.xml
使用站点地图索引. 这是包含 WordPress 站点公开的所有站点地图页面列表的主要 XML 文件。默认此功能是开启状态,如果你的站点打开是404,那么可能是主题或插件禁止了它,可以使用wp_sitemaps_enabled
来开启:
add_filter( 'wp_sitemaps_enabled', '__return_true' );
站点地图索引最多可包含 50000 个站点地图,单个站点地图最多可包含 2000 个(可过滤的)条目。
默认情况下,所有帖子类型、分类法和用户都会生成 XML 站点地图。Oxygen模板也会包含在站点地图中:https://example.com/wp-sitemap-posts-ct_template-1.xml,这是没有意义的,因为这些模板并不意味着面向公众。

禁用Oxygen氧气模板的 WP 站点地图
使用Oxygen创建的网站,首先要排除这条链接: https://example.com/wp-sitemap-posts-ct_template-1.xml,使用代码片段插件添加如下PHP代码
add_filter(
'wp_sitemaps_post_types',
function( $post_types ) {
unset( $post_types['ct_template'] );
return $post_types;
}
);
如果您希望禁用多种帖子类型的 XML 站点地图,只需将它们添加到新行中,unset()
如下所示:
add_filter(
'wp_sitemaps_post_types',
function( $post_types ) {
unset( $post_types['ct_template'] );
unset( $post_types['surl'] );
return $post_types;
}
);
禁用作者的 WP 站点地图
大多数的网站都不需要作者的地图链接 https://www.acerackingsystem.com/wp-sitemap-users-1.xml,PHP排除代码如下:
add_filter( 'wp_sitemaps_add_provider', function ($provider, $name) {
return ( $name == 'users' ) ? false : $provider;
}, 10, 2);
从站点地图中排除某个特定的帖子
我们常常会有一些特定的帖子,不希望被搜索引擎收录的,那么最好也将它们从站点地图中排除。
过滤器wp_sitemaps_posts_query_args
可用于从站点地图中排除特定帖子。下面是我的一个商城,排除购物车和结账页面的例子,将下面的代码加到子主题的functions.php,亲测有效:
// disable cart and check out page
function disable_sitemap_specific_page($args, $post_type) {
if ('page' !== $post_type) return $args;
$args['post__not_in'] = isset($args['post__not_in']) ? $args['post__not_in'] : array();
$args['post__not_in'][] = 23;
$args['post__not_in'][] = 24;
return $args;
}
add_filter('wp_sitemaps_posts_query_args', 'disable_sitemap_specific_page', 10, 2);
下面是自定义站点地图排除帖子,页面,产品等PHP示例
<?php /* Template Name: Sitemap */ get_header(); ?>
<section id="sitemap">
<div class="sitemap-heading">
<h1>Sitemap</h1>
</div>
<!-- START WordPress Posts Loop -->
<div class="sitemap-posts">
<h2>Posts</h2>
<ul>
<?php
$wp_posts = new WP_Query(
array(
'post_type' => 'post', // slug for posts
'posts_per_page' => -1, // -1 shows all posts
'post_status' => 'publish', // only shows published posts
'post__not_in' => array( 1, 2, 3 ) // Enter post ids here to exclude posts
'orderby' => 'title', // orders by post title
'order' => 'ASC' // orders post title alphabetically
)
);
?>
<?php while ( $wp_posts->have_posts() ) : $wp_posts->the_post(); ?>
<li>
<a href="<?php echo get_permalink(get_the_ID()); ?>" rel="dofollow" title="<?php the_title(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
</div>
<!-- END WordPress Posts Loop -->
<!-- START WordPress Pages Loop -->
<div class="sitemap-pages">
<h2>Pages</h2>
<ul>
<?php
$wp_pages = new WP_Query(
array(
'post_type' => 'page', // slug for pages
'posts_per_page' => -1, // -1 shows all pages
'post_status' => 'publish', // only shows published pages
'post__not_in' => array( 1, 2, 3 ) // Enter post ids here to exclude pages
'orderby' => 'title', // orders by page title
'order' => 'ASC' // orders page title alphabetically
)
);
?>
<?php while ( $wp_pages->have_posts() ) : $wp_pages->the_post(); ?>
<li>
<a href="<?php echo get_permalink(get_the_ID()); ?>" rel="dofollow" title="<?php the_title(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
</div>
<!-- END WordPress Pages Loop -->
<!-- START WordPress Custom Post Type Loop - Delete this section if you are not using Custom Post Types -->
<div class="sitemap-custom-post-types">
<h2>Custom Post Type</h2>
<ul>
<?php
$wp_cpt = new WP_Query(
array(
'post_type' => 'cpt-slug', // Change this to the slug of your Custom Post Type (cpt)
'posts_per_page' => -1, // -1 shows all cpt posts
'post_status' => 'publish', // only shows published cpt pages
'post__not_in' => array( 1, 2, 3 ) // Enter post ids here to exclude custom posts
'orderby' => 'title', // orders by cpt post title
'order' => 'ASC' // orders cpt posts title alphabetically
)
);
?>
<?php while ( $wp_cpt->have_posts() ) : $wp_cpt->the_post(); ?>
<li>
<a href="<?php echo get_permalink(get_the_ID()); ?>" rel="dofollow" title="<?php the_title(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
</div>
<!-- END WordPress Custom Post Type Loop - Delete this section if you are not using Custom Post Types -->
<!-- START WordPress Multiple Custom Post Type Loop - Delete this section if you are not using Custom Post Types -->
<div class="sitemap-custom-post-types">
<h2>Multiple Custom Post Types</h2>
<ul>
<?php
$wp_multiple_cpts = new WP_Query(
array(
'post_type' => array('cpt-slug-1', 'cpt-slug-2', 'cpt-slug-3'), // Change this to the slug of your Custom Post Type (cpt)
'posts_per_page' => -1, // -1 shows all cpt posts
'post_status' => 'publish', // only shows published cpt pages
'post__not_in' => array( 1, 2, 3 ) // Enter post ids here to exclude custom posts
'orderby' => 'title', // orders by cpt post title
'order' => 'ASC' // orders cpt posts title alphabetically
)
);
?>
<?php while ( $wp_multiple_cpts->have_posts() ) : $wp_multiple_cpts->the_post(); ?>
<li>
<a href="<?php echo get_permalink(get_the_ID()); ?>" rel="dofollow" title="<?php the_title(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
</div>
<!-- END WordPress Multiple Custom Post Type Loop - Delete this section if you are not using Custom Post Types -->
<!-- START WooCommerce Products Loop - Delete this section if you are not using WooCommerce -->
<div class="sitemap-woocommerce-products">
<h2>Products</h2>
<ul>
<?php
$woo_products = new WP_Query(
array(
'post_type' => 'product', // slug of woocommerce products
'posts_per_page' => -1, // -1 shows all products
'post_status' => 'publish', // only shows published products
'post__not_in' => array( 1, 2, 3 ) // Enter post ids here to exclude products
'orderby' => 'title', // orders by product title
'order' => 'ASC' // orders product title alphabetically
)
);
?>
<?php while ( $woo_products->have_posts() ) : $woo_products->the_post(); ?>
<li>
<a href="<?php echo get_permalink(get_the_ID()); ?>" rel="dofollow" title="<?php the_title(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
</div>
<!-- END WooCommerce Products Loop - Delete this section if you are not using WooCommerce -->
</section>
<?php get_footer(); ?>