2017-03-07 61 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檔案。

相關問題