WordPress 自带网站地图功能的开启和关闭及功能自定义设置

大约从WP5.5版本开始,WordPress就会自带网站地图SITEMAP功能,这样我们就不需要用额外的插件实现网站的地图。当然,如果我们有特殊地图的插件用的不错,也可以将 WordPress 自带的地图功能禁止。

add_filter( 'wp_sitemaps_enabled', '__return_false' );

默认是开启的,我们可以用这个代码关闭。

同时,我个人是喜欢用自带的地图的。这里我们还可以通过一些脚本控制地图的设置。

1、地图内容数量

add_filter( 'wp_sitemaps_max_urls', '__wp_sitemaps_max_urls' );
function __wp_sitemaps_max_urls(){
    return 2000;
}

默认是控制2000条的,我们可以设置更多。

2、禁用用户站点地图

// disable users sitemap
function shapeSpace_disable_sitemap_users($provider, $name) {
  return ($name == 'users') ? false : $provider;
}
add_filter('wp_sitemaps_add_provider', 'shapeSpace_disable_sitemap_users', 10, 2);

3、禁用文章类型站点地图

// disable post type sitemap
function shapeSpace_disable_sitemap_post_types($post_types) {
  unset($post_types['page']); // 可以修改page为你需要的自定义文章类型
  return $post_types;
}
add_filter('wp_sitemaps_post_types', 'shapeSpace_disable_sitemap_post_types');

4、禁用分类法站点地图

// disable taxonomy sitemap
function shapeSpace_disable_sitemap_taxonomy($taxonomies) {
  unset($taxonomies['post_tag']); // can be post_tag, category, post_format, or any taxonomy
  return $taxonomies;
}
add_filter('wp_sitemaps_taxonomies', 'shapeSpace_disable_sitemap_taxonomy');

5、从站点地图中排除特定页面

/ disable specific page
function shapeSpace_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'][] = 2; // exclude page with ID = 2
  return $args;
}
add_filter('wp_sitemaps_posts_query_args', 'shapeSpace_disable_sitemap_specific_page', 10, 2);

6、排除多个页面内容

// disable specific pages
function shapeSpace_disable_sitemap_specific_pages($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'][] = 2; // exclude page with ID = 2
  $args['post__not_in'][] = 3; // exclude page with ID = 3
  $args['post__not_in'][] = 4; // exclude page with ID = 4
  $args['post__not_in'][] = 5; // exclude page with ID = 5
  $args['post__not_in'][] = 6; // exclude page with ID = 6
  return $args;
}
add_filter('wp_sitemaps_posts_query_args', 'shapeSpace_disable_sitemap_specific_pages', 10, 2);

7、排除特定文章内容

// disable specific post
function shapeSpace_disable_sitemap_specific_post($args, $post_type) {
  if ('post' !== $post_type) return $args;
  $args['post__not_in'] = isset($args['post__not_in']) ? $args['post__not_in'] : array();
  $args['post__not_in'][] = 1; // exclude post with ID = 1
  return $args;
}
add_filter('wp_sitemaps_posts_query_args', 'shapeSpace_disable_sitemap_specific_post', 10, 2);

8、排除指定文章内容

// disable specific posts
function shapeSpace_disable_sitemap_specific_posts($args, $post_type) {
  if ('post' !== $post_type) return $args;
  $args['post__not_in'] = isset($args['post__not_in']) ? $args['post__not_in'] : array();
  $args['post__not_in'][] = 1; // exclude post with ID = 1
  $args['post__not_in'][] = 2; // exclude post with ID = 2
  $args['post__not_in'][] = 3; // exclude post with ID = 3
  $args['post__not_in'][] = 4; // exclude post with ID = 4
  $args['post__not_in'][] = 5; // exclude post with ID = 5
  return $args;
}
add_filter('wp_sitemaps_posts_query_args', 'shapeSpace_disable_sitemap_specific_posts', 10, 2);

这样,我们就可以通过地图索引控制展现的内容。

4.7/5 - (10 votes)
© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享