【问题标题】:How to remove the WordPress generated title tag for specific custom post type?如何删除 WordPress 为特定自定义帖子类型生成的标题标签?
【发布时间】:2016-09-27 18:56:52
【问题描述】:

好的,所以我继承了另一个开发人员构建的网站,并且代码到处都是。这是一个按州/市组织的目录网站,为了确保相同的城市名称可以作为不同的帖子出现在不同的州,我们实施了一些时髦的重写和重定向。

长话短说,由于这些 url 重写,All In One SEO 不适用于自定义帖子类型。

我通过为自定义帖子类型创建第二个 header.php 并实现以下内容来管理解决方法:

<?php while ( have_posts() ) : the_post(); ?>
<?php
    // get the state name
    $state = '';
    $states = wp_get_post_terms( get_the_ID(), 'state' );
    if( $states ) {
        $state = ', ' . strtoupper( $states[0]->description );
    }
    $city_name = get_the_title();
    $title = $city_name . $state;
?>

<title>Cash for Gold Near <?php the_title(); ?> - Find Gold Buyers Near <?php the_title(); ?></title>
<?php endwhile; ?>

它会生成适当的标题标签,但后来我遇到了它们是两个标题标签的问题 - 自定义标签和 WP 生成的标签。

有没有办法摆脱 WP 为特定自定义帖子类型生成的标题标签(以及元描述)?

感谢任何帮助。

谢谢!

辛西娅

【问题讨论】:

  • 您需要找到正在创建标题的其他模板。 Wordpress 不只是这样做,还必须有一个类似于生成它的功能。我将从查看 header.php 开始。您可以在选择要显示的帖子类型之前检查帖子类型。
  • 自动生成的标题由functions.php中的以下函数实现: function av_theme_slug_setup() { add_theme_support( 'title-tag' );我无法将其剥离,因为它会在整个站点范围内剥离它。如何为自定义帖子类型禁用该特定功能?
  • 我用代码添加了答案。

标签: php wordpress custom-post-type


【解决方案1】:

为生成其他标题标签的帖子类型添加检查:

if( !is_singular( 'post-type-slug' ) ){
     //write normal post title
}

您应该将“post-type-slug”替换为用于您的帖子类型的 slug。您可以在其他区域执行相同的操作,仅在正确的帖子类型时才显示标题:

if( is_singular( 'post-type-slug' ) ){
     //write custom post title
}

【讨论】:

    【解决方案2】:

    改为:

    <title>Cash for Gold Near <?php echo $title; ?> - Find Gold Buyers Near <?php echo $title; ?></title>
    

    【讨论】:

    • 这行不通,因为以前的开发人员如何处理自定义帖子类型 URL 重写。这真的是歪斜的,我上面使用的代码工作正常。我只需要禁用 WP 生成的标题标签和元描述,这样我就没有两个。
    • 您可以找到生成另一个标题的代码并将其放置在条件块中。如果帖子类型是自定义的,则回显 $post->title 否则回显 get_the_title()
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-30
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    • 2013-10-25
    • 1970-01-01
    相关资源
    最近更新 更多