我有一个 WordPress 网站,它使用 Yoast SEO 来管理 规范链接 和 noindex 元机器人,除此之外,还有一个非常特殊的配置。
默认情况下,Yoast 的插件会添加一个 <link rel="canonical" href="...">
在每一页。我对在每个静态页面(无论其类型是什么)中显示规范链接不感兴趣,而是在某些类型的帖子中显示规范链接:Pages 和 Posts(不是附件、类别、存档等)
我知道防止 Yoast 默认添加规范链接的方法,只需在 functions.php 中添加以下代码:add_filter( 'wpseo_canonical', '__return_false' );
.但是,如果我只想在某些帖子类型中传递该过滤器怎么办?或者在除了某些类型的帖子之外的每个页面中传递它(这两种方法对我都很有用)。帮助将不胜感激。有吗?
更新:有一些小修复的有效答案
function remove_canonical_from_post_types($url) {
$post_type = get_post_type();
if ($post_type !== 'post' && $post_type !== 'page') { // If post type is not Post nor Page, doesn't add a canonical link in any case
return false;
}
else { // It is Post or Page
if (is_category() || is_author() || is_tag() || is_archive()) { // If page is post type 'Post' we don't want to add canonical in some sub-types: Category, Author, Tag, Archive
return false;
}
return $url; // In any other case (Posts and Pages) adds a canonical link
}
}
add_filter('wpseo_canonical', 'remove_canonical_from_post_types');
最佳答案
尝试使用提供的“wpseo_canonical”过滤器并在那里检查正确的帖子类型(并在那里返回 false)。
像这样:
function remove_canonical_from_post_types($url)
{
$post_type = get_post_type();
if ($post_type !== 'post' && $post_type !== 'page') {
return false;
}
return $url;
}
add_filter('wpseo_canonical', 'remove_canonical_from_post_types');
关于php - 如何选择帖子类型(WordPress)以通过头部过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34946982/