停止wordpress自定义帖子类型出现在搜索引擎中


Stop wordpress custom post type from appearing in search engines

我为我当前的项目创建了一个名为"Company"的新帖子类型。现在的问题是,谷歌开始索引我的公司页面,如下:www.domain.com/company-name

我想保守这个地区的秘密,不被任何搜索引擎列出。我遇到了这个"publicly_queryable"arg。用于register_post_type函数。但我不确定这是否会按照我想要的方式进行。

我不想为此使用任何插件。

在该页面上,您可以包含元标签,该标签告诉机器人不要索引页面:

<meta name="robots" content="noindex, nofollow" />

或者,你可以在你的域的基础上制作一个robots.txt文件,告诉他们也不要索引所述页面(爬虫应该查找这个文件):

User-agent: *
Disallow: /company-name

如果你想用一个函数来做这件事,你可以在functions.php文件中添加这样的东西来添加noindex标记:

function noindex_for_companies()
{
    if ( is_singular( 'company' ) ) {
        return '<meta name="robots" content="noindex, follow">';
    }
}
add_action('wp_head', 'noindex_for_companies');

如果company不同,请将其替换为您的自定义帖子类型

请注意,当有人拥有URL时,dis不会隐藏帖子,它只是鼓励搜索引擎不要对其进行索引。

上面的答案是正确的,但函数应该echo不返回:

function noindex_for_companies()
{
    if ( is_singular( 'company' ) ) {
        echo '<meta name="robots" content="noindex, follow">';
    }
}
add_action('wp_head', 'noindex_for_companies');

一个更好的方法可以是使用wp_robots()过滤器,如下所述。

add_filter( 'wp_robots', function( $robots ) {
  if ( is_singular( 'company' ) ) {
    $robots['noindex']  = true;
    $robots['nofollow'] = true;
  }
  return $robots;
} );