【问题标题】:How to filter post type title properly on post type archive?如何在帖子类型存档中正确过滤帖子类型标题?
【发布时间】:2017-07-27 10:16:48
【问题描述】:

我已使用此代码过滤自定义帖子类型的$title,当有搜索结果时它工作得非常好,例如在巴黎销售的梅赛德斯奔驰汽车的查询将显示$title 像这样@987654323 @

add_filter('wpseo_title', 'vehicle_listing_title');
function vehicle_listing_title( $title ) 
{
  if ( get_post_type() == 'vehicles' )
  {
    $location = get_the_terms($post->ID, 'vehicle_location');
    $model = get_the_terms($post->ID, 'vehicle_model');
    $title = $model[0]->name . 'used cars for sale in' . $location[0]->name .'on'. get_bloginfo('name');
  }
  return $title;
}

但话又说回来,当没有任何搜索结果时,代码会返回单个术语标题,例如,查询在巴黎出售的梅赛德斯奔驰汽车将显示 $title,就像这样 Mercedes Benz archives – Sitename 我需要这个来为所有搜索查询返回过滤的$title,因为我有一个站点地图从自定义帖子类型中提取这些链接。因此,出于 SEO 的原因,我显然需要它来发挥作用。

我已经为此苦苦挣扎了一段时间。 任何帮助将不胜感激

【问题讨论】:

    标签: wordpress filter seo title custom-post-type


    【解决方案1】:

    你不只需要一个 else 语句吗?

    add_filter('wpseo_title', 'vehicle_listing_title');
    
    function vehicle_listing_title( $title ){
    
       if ( get_post_type() == 'vehicles' ){
          $location = get_the_terms($post->ID, 'vehicle_location');
          $model = get_the_terms($post->ID, 'vehicle_model');
          $title = $model[0]->name . 'used cars for sale in' . $location[0]->name .'on'. get_bloginfo('name');
      }else{
         $title = 'Sorry there are no' . $model[0]->name . 'used cars for sale in' . $location[0]->name .'on'. get_bloginfo('name');
      }
    
    return $title;
    }
    

    如果这不起作用,也许这会:

    elseif(/*there are no results or post_count = 0 */)
    

    抱歉 elseif 含糊不清,但我不确定您是如何获得结果的,是搜索还是 CPT 存档。

    【讨论】:

      猜你喜欢
      • 2014-11-28
      • 2013-06-02
      • 1970-01-01
      • 2019-07-14
      • 1970-01-01
      • 2020-06-06
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      相关资源
      最近更新 更多