WordPress 自帶網(wǎng)站地圖功能的開啟和關(guān)閉及功能自定義設(shè)置

麥子 優(yōu)化維護67字?jǐn)?shù) 2930閱讀9分46秒閱讀模式

大約從WP5.5版本開始,WordPress就會自帶網(wǎng)站地圖SITEMAP功能,這樣我們就不需要用額外的插件實現(xiàn)網(wǎng)站的地圖。當(dāng)然,如果我們有特殊地圖的插件用的不錯,也可以將 WordPress 自帶的地圖功能禁止。

add_filter( 'wp_sitemaps_enabled', '__return_false' );

默認(rèn)是開啟的,我們可以用這個代碼關(guān)閉。

同時,我個人是喜歡用自帶的地圖的。這里我們還可以通過一些腳本控制地圖的設(shè)置。

1、地圖內(nèi)容數(shù)量

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

默認(rèn)是控制2000條的,我們可以設(shè)置更多。

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、排除多個頁面內(nèi)容

// 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、排除特定文章內(nèi)容

// 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、排除指定文章內(nèi)容

// 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);

這樣,我們就可以通過地圖索引控制展現(xiàn)的內(nèi)容。

投上你的一票
 
  • 本文由 麥子 發(fā)表于 2024年12月18日 16:28:00
  • 轉(zhuǎn)載請務(wù)必保留本文鏈接:http://bjj.org.cn/wpmap-able.html
  • WordPress地圖控制