php - 如何选择帖子类型(WordPress)以通过头部过滤器?

标签 php wordpress seo canonical-link

我有一个 WordPress 网站,它使用 Yoast SEO 来管理 规范链接noindex 元机器人,除此之外,还有一个非常特殊的配置。

默认情况下,Yoast 的插件会添加一个 <link rel="canonical" href="...">在每一页。我对在每个静态页面(无论其类型是什么)中显示规范链接不感兴趣,而是在某些类型的帖子中显示规范链接:PagesPosts(不是附件、类别、存档等)

我知道防止 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/

相关文章:

php - 运行插入查询时Mysql错误问题

php - 在 PHP/MySQL echo URL 变量中检查/插入 http

php - iOS - JSONString 到 PHP

css - 如何识别 CSS 中的导航栏颜色

java - HTMLUnit 和 AppEngine

.htaccess - 301 仅在特定 url 上重定向,同时使用 seo 友好重写

asp.net - 如何让 Response.Redirect 指定 rel ="nofollow"?

php - 哪个更快,使用更多 PHP 的大连接查询或使用更少 PHP 的多个小选择?

html - 对于超过一页的内容,页脚保留在屏幕底部位于顶部滚动的位置

css - 更改与父文本颜色不同的子文本颜色?