先前處理一個案子發現產品首頁使用多語網站串接時,切換其他二個語系會fallback回中文的標題與描述。
感覺是某個模版的關連壞掉了。
可以簡單透過以下二個filter hook解決。
add_filter( 'rank_math/frontend/title', function( $title )
add_filter( 'rank_math/frontend/description', function( $desc )
完整的sample可以參考如下
add_filter( 'rank_math/frontend/title', function( $title ) {
if ( function_exists('is_shop') && is_shop() ) {
$lang = apply_filters( 'wpml_current_language', null );
$sep = RankMath\Helper::get_settings( 'titles.separator' ) ?: '-';
$sitename = get_bloginfo( 'name' );
$custom_titles = [
'zh-hant' => '產品',
'ja' => '製品',
'en' => 'Products',
];
return ($custom_titles[$lang] ?? $title) . " {$sep} {$sitename}";
}
return $title;
}, 20);
add_filter( 'rank_math/frontend/description', function( $desc ) {
if ( function_exists('is_shop') && is_shop() ) {
$lang = apply_filters( 'wpml_current_language', null );
$custom_desc = [
'zh-hant' => '明昌輪業產品一覽,明昌致力於生產及開發滾子鏈條、輸送鏈條、特殊鏈條,歡迎諮詢!',
'ja' => 'MAXTOP チェーンメーカー、お気軽にお問い合わせを!',
'en' => 'MCC Product Overview – MCC is dedicated to the production and development of roller chains, conveyor chains, and special chains. Feel free to contact us!',
];
return $custom_desc[$lang] ?? $desc;
}
return $desc;
}, 20);