2017-03-07 92 views
0

我已经使用这个代码,以筛选自定义后类型的$title,当有搜索结果时,工作完全正常如查询奔驰轿车在巴黎发售会显示一个$title这样Mercedes Benz used cars for sale in Paris on Sitename如何在帖子类型档案中过滤帖子类型标题?

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的原因。

我一直在为此奋斗了一段时间。 任何帮助,将不胜感激

回答

0

你不只是需要一个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档案。

相关问题