Profile Image

新编程

xinbc.com

Yoast SEO XML Sitemaps API 接口文档

虽然Yoast SEO为XML站点地图提供了合理的默认行为(以及用于包含/排除的UI控件),但自定义主题或插件有时需要更改我们的标记或逻辑。在这些情况下,您可以使用下面的示例来修改我们的网站地图XML Sitemaps的生成和输出方式。

排除内容类型

排除特定文章

/*** 从XML站点地图中排除发布的文章** @return 返回文章ID数组*/
function exclude_posts_from_xml_sitemaps() {
return [ 1, 2, 3 ];
}

add_filter( ‘wpseo_exclude_from_sitemap_by_post_ids’, ‘exclude_posts_from_xml_sitemaps’ );

排除特定类型页面

/**
* 从XML站点地图中排除发布的页面类型.
* @boolean型参数 $excluded 是否默认排除页面类型.
* @string型参数$post_type 要排除的页面类型.
* @返回bool型是否应排除给定的页面类型
*/
function sitemap_exclude_post_type( $excluded, $post_type ) {
return $post_type === ‘recipes’;
}

add_filter( ‘wpseo_sitemap_exclude_post_type’, ‘sitemap_exclude_post_type’, 10, 2 );

排除分类目录

/**
* 从XML站点地图中排除分类目录.
* @boolean型参数 $excluded 是否默认排除分类.
* @string 型参数$taxonomy要排除的分类.
* @返回bool 型是否应排除给定的分类.
*/
function sitemap_exclude_taxonomy( $excluded, $taxonomy ) {
return $taxonomy === ‘ingredients’;
}

add_filter( ‘wpseo_sitemap_exclude_taxonomy’, ‘sitemap_exclude_taxonomy’, 10, 2 );

 

排除作者author

/**
* 从XML站点地图中排除发布的ID为5等级的作者.
* @array型参数$users要筛选的用户对象数组.
* @返回array型参数是否保留作者.
*/
function sitemap_exclude_authors( $users ) {
return array_filter( $users, function( $user ) {
if ( $user->ID === 5 ) {
return false;
}

return true;
} );
}

add_filter( ‘wpseo_sitemap_exclude_author’, ‘sitemap_exclude_authors’, 10, 1 );

Exclude a taxonomy term
/**
* 从XML站点地图中排除发布的ID为3和11等级的作者
* @array型参数$terms要排除的分类ID号.
* @返回array.
*/
function sitemap_exclude_terms( $terms ) {
return [ 3, 11 ];
}

add_filter( ‘wpseo_exclude_from_sitemap_by_term_ids’, ‘sitemap_exclude_terms’, 10, 1 );

添加内容类型

添加定制内容类型

/**
* XML sitemap添加一个定制发布内容类型
*/
function enable_custom_sitemap() {
global $wpseo_sitemaps;
if ( isset( $wpseo_sitemaps ) && ! empty ( $wpseo_sitemaps ) ) {
$wpseo_sitemaps->register_sitemap( ‘recipe’, ‘create_recipe_sitemap’ );
}
}

add_action( ‘init’, ‘enable_custom_sitemap’ );

XML sitemaps添加additional/external 索引
function add_sitemap_custom_items( $sitemap_custom_items ) {
$sitemap_custom_items .= ‘
<sitemap>
<loc>http://www.example.com/external-sitemap-1.xml</loc>
<lastmod>2017-05-22T23:12:27+00:00</lastmod>
</sitemap>’;
return $sitemap_custom_items;
}

add_filter( ‘wpseo_sitemap_index’, ‘add_sitemap_custom_items’ );

Alter the URL of an entry
/**
* Alters the URL structure for an example custom post type.
*
* @param string $url The URL to modify.
* @param WP_Post $post The post object.
*
* @return string The modified URL.
*/
function sitemap_post_url( $url, $post ) {
if ( $post->post_type === ‘guest_authors’ ) {
return \str_replace( ‘guest-authors’, ‘guests’, $url );
}

return $url;
}

add_filter( ‘wpseo_xml_sitemap_post_url’, ‘sitemap_post_url’, 10, 2 );

Alter the number of sitemap entries
/**
* Alters the number of entries in each XML sitemap.
*
* @return integer The maximum entries per sitemap.
*/
function max_entries_per_sitemap() {
return 100;
}

add_filter( ‘wpseo_sitemap_entries_per_page’, ‘max_entries_per_sitemap’ );

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注