WordPress已经自带了sitemap功能,之前的Core Sitemaps插件实现已经不需要,直接安装版本等于或者高于WordPress 5.5的版本即可开启。
如果你启用了其他插件,可能自带的sitemap功能会被屏蔽,并且wp-sitemap.xml
这个地址会被重定向。
目前sitemap中不会包含:图片、视频、新闻格式,参考如下:
Image/Video/News Sitemaps:
WordPress currently implements and supports the core sitemaps format as defined on sitemaps.org. Sitemap extensions like image, video, and news sitemaps are not covered by this feature, as these are usually only useful for a small number of websites. In future versions of WordPress, filters and hooks may be added to enable adding such functionality. For now this will still be left to plugins to implement.
但是官方表示未来的版本中,可能会添加过滤器和挂钩以启用此类功能,目前要支持Image/Video/News形式必须使用第三方插件实现。
下面是整理的自带sitemap的常见功能详情:
1、如何加上最后更新时间?
默认sitemap xml里面只有地址,并没有更新时间:
如何加入时间?在functions.php
中加入:
add_filter( 'wp_sitemaps_posts_entry', function( $entry, $post ) { $entry['lastmod'] = $post->post_modified_gmt; return $entry; }, 10, 2 );
2、禁用WordPress自带站点地图
add_filter( 'wp_sitemaps_enabled', '__return_false' );
3、不包含page页面
add_filter( 'wp_sitemaps_post_types', function( $post_types ) { unset( $post_types['page'] ); return $post_types; } );
4、不包含tag标签页面
add_filter( 'wp_sitemaps_taxonomies', function( $taxonomies ) { unset( $taxonomies['post_tag'] ); return $taxonomies; } );
5、增加 changefreq, priority标签信息
add_filter( 'wp_sitemaps_posts_entry', function( $entry, $post ) { $entry['changefreq'] = 'Daily'; $entry['priority'] = '0.6'; return $entry; }, 10, 2 );