わかる人向け。
woo commerceの商品登録でメインカテゴリを設定してるのにそのカテゴリがパンくず表示に反映されないという地味な問題に対してのおそらく一番簡単な対応方法。
喜ぶ担当者の笑顔を思い出して解決できたわー。
その方には焼肉もおごってもらってますし執念でなんとか。
Woo commerceのアップデートでそのうち直ると思うけど。。
不具合が起こった状況のバージョンは、
Wordpress 4.9.8
Woo commerceバージョン 3.5.1
1. Yoast SEOというプラグインをインストール
メインカテゴリ(primary category)を取得するための関数yoast_get_primary_term_idを使うためにYoast SEOをワードプレスにインストール。
SEOプラグインとしても1番のおすすめなので、多分インストールしてる人も多いはず。
ちなみに、Wordpressのパンくずリスト表示はいつもこのプラグインのものを使ってる。
2. wordpressテーマのfunctions.phpに下記のコードを追記
/wp-content/themes/{theme name}/functions.phpに下記のコードを追記。
うまく思い通りに動かないwoocommerce_breadcrumbを上書きしちゃう。
これで不具合が起こるかは確認必要。
表示HTMLはいい感じに修正してもらえれば。
// show primary category above of title in product page
function woocommerce_breadcrumb($args = array()){
if(function_exists('yoast_get_primary_term_id')){
$primary_term_product_id = yoast_get_primary_term_id('product_cat');
$postProductTerm = get_term( $primary_term_product_id );
//If the post type is a product, display the Primary Category Link
if ( 'product' === get_post_type() ) {
if ( $postProductTerm && ! is_wp_error( $postProductTerm ) ) {
echo '<a href="' . esc_url( get_term_link( $postProductTerm->term_id ) ) . $postProductTerm->slug . '">';
echo $postProductTerm->name;
echo '</a>';
}
}
}
}
via How to Display the Primary Category for a Product or Post In WordPress Loop | Travis Media
↑ このページに感謝 ????