从上一个网站地图中排除URL



Yoast生成的站点地图中有几个URL。

我找不到这个页面,因为它很久以前就被删除了,重定向到另一个页面很好。

我发现了一个代码片段,它只适用于页面或帖子ID。由于我不知道页面ID,有可能通过slug删除它吗?

适用于页面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' );

我需要删除看起来像的URL

https://website/example2
https://website/example3
https://website/example4

如果知道页面的slug,可以使用get_page_by_path((函数检索它们的ID。

你的代码可以是这样的:

function exclude_posts_from_xml_sitemaps() {
$slugs = array('example1', 'example2', 'example3');
$ids = array();
foreach ( $slugs as $slug ) {
$page = get_page_by_path( $slug );
if ( $page !== null ) {
$ids[] = $page->ID;
}
}
return $ids;
}

最新更新