wp_sitemaps_index_entry
云策文档标注
概述
wp_sitemaps_index_entry 是一个 WordPress 过滤器钩子,用于修改站点地图索引中的条目。它允许开发者在生成站点地图索引时自定义条目数据,例如调整 URL 或其他属性。
关键要点
- 这是一个过滤器钩子,用于过滤站点地图索引的条目。
- 参数包括 $sitemap_entry(站点地图条目数组)、$object_type(对象类型名称)、$object_subtype(对象子类型名称)和 $page(页码)。
- 在 WordPress 5.5.0 版本中引入。
- 常用于多站点环境中调整站点地图的 URL。
代码示例
add_filter( 'wp_sitemaps_index_entry', 'wpdocs_sitemaps_index_entry_callback' );
function wpdocs_sitemaps_index_entry_callback( $sitemap_entry ) {
switch_to_blog( get_blog_id_from_url( $_SERVER['HTTP_HOST'] ) );
$link_array = explode( '/', $sitemap_entry['loc'] );
$sitemap_url_element = end( $link_array );
$sitemap_entry['loc'] = home_url( $sitemap_url_element );
return $sitemap_entry;
}注意事项
- 确保在回调函数中正确处理参数,特别是 $sitemap_entry 数组,以避免破坏站点地图结构。
- 在多站点环境中使用此钩子时,注意切换博客上下文,以确保 URL 正确对应当前站点。
原文内容
Filters the sitemap entry for the sitemap index.
Parameters
$sitemap_entryarray-
Sitemap entry for the post.
$object_typestring-
Object empty name.
$object_subtypestring-
Object subtype name.
Empty string if the object type does not support subtypes. $pageint-
Page number of results.
Source
$sitemap_entry = apply_filters( 'wp_sitemaps_index_entry', $sitemap_entry, $this->object_type, $type['name'], $page );
Changelog
| Version | Description |
|---|---|
| 5.5.0 | Introduced. |
Skip to note 2 content
Krunal Bhimajiyani
To alter the home URL for the sitemap index in WP Multisites,
add_filter( 'wp_sitemaps_index_entry', 'wpdocs_sitemaps_index_entry_callback' ); function wpdocs_sitemaps_index_entry_callback( $sitemap_entry ) { switch_to_blog( get_blog_id_from_url( $_SERVER['HTTP_HOST'] ) ); $link_array = explode( '/', $sitemap_entry['loc'] ); $sitemap_url_element = end( $link_array ); $sitemap_entry['loc'] = home_url( $sitemap_url_element ); return $sitemap_entry; }