概要
woocommerce_layered_nav_any_label
フィルタは、WooCommerceのレイヤードナビゲーションにおいて、特定の属性やタームのラベルをカスタマイズするために使用されます。このフィルタは、主に製品のフィルタリング機能をかける際に役立ち、カスタマーエクスペリエンスを向上させるために利用されます。具体的には、以下のような場面でよく使われます。
- 属性ラベルの多国語対応
- 特定の条件に基づいたラベルのカスタマイズ
- SEO強化のためのカスタムラベル生成
- プロモーションに応じたダイナミックなラベル表示
- 商品の在庫状況に応じたラベル変化
- ユーザー体験向上のためのカスタマイズ
構文
apply_filters( 'woocommerce_layered_nav_any_label', $label, $term, $taxonomy );
パラメータ
$label
: カスタマイズされるラベルの文字列$term
: 対象のタームオブジェクト$taxonomy
: タクソノミー名
戻り値
- カスタマイズされたラベルの文字列
使用可能なバージョン
- WooCommerce: 3.0以降
- WordPress: 4.5以降
この関数のアクションでの使用可能性
アクション | 使用可能 |
---|---|
mu_plugin_loaded | |
registered_post_type | |
plugins_loaded | |
wp_roles_init | |
setup_theme | |
after_setup_theme | |
set_current_user | |
init | |
register_sidebar | |
wp_loaded | |
send_headers | |
parse_query | |
pre_get_posts | |
wp | |
template_redirect | |
get_header | |
wp_head |
サンプルコード
サンプル1: カスタムラベルを追加する
このサンプルコードでは、特定の条件に基づいてカスタムラベルを追加します。
add_filter( 'woocommerce_layered_nav_any_label', 'custom_layered_nav_label', 10, 3 );
function custom_layered_nav_label( $label, $term, $taxonomy ) {
if ( $taxonomy === 'pa_color' && $term->name === 'Red' ) {
return 'Bright Red';
}
return $label;
}
引用元: https://developer.wordpress.org
サンプル2: 多国語サポートを実装
このコードは、複数の言語に対応したラベルを生成します。
add_filter( 'woocommerce_layered_nav_any_label', 'multilingual_layered_nav_label', 10, 3 );
function multilingual_layered_nav_label( $label, $term, $taxonomy ) {
switch ( get_locale() ) {
case 'es_ES':
return 'Etiqueta en Español: ' . $label;
case 'fr_FR':
return 'Étiquette en Français: ' . $label;
default:
return $label;
}
}
引用元: https://developer.wordpress.org
サンプル3: 在庫状況に基づいてラベルを変える
このサンプルでは、在庫がない場合に特別なラベルを表示します。
add_filter( 'woocommerce_layered_nav_any_label', 'out_of_stock_layered_nav_label', 10, 3 );
function out_of_stock_layered_nav_label( $label, $term, $taxonomy ) {
if ( ! $term->count ) {
return $label . ' - Out of Stock';
}
return $label;
}
引用元: https://developer.wordpress.org
サンプル4: プロモーションラベルを表示
このコードは、特定のプロモーションに応じたラベルを追加します。
add_filter( 'woocommerce_layered_nav_any_label', 'promotion_layered_nav_label', 10, 3 );
function promotion_layered_nav_label( $label, $term, $taxonomy ) {
if ( $taxonomy === 'pa_sale' ) {
return 'Special Offer: ' . $label;
}
return $label;
}
引用元: https://developer.wordpress.org
サンプル5: デフォルトラベルを変更
このサンプルでは、デフォルトのラベルを全て「新着商品」に変更します。
add_filter( 'woocommerce_layered_nav_any_label', 'default_layered_nav_label', 10, 3 );
function default_layered_nav_label( $label, $term, $taxonomy ) {
return 'New Arrival';
}
引用元: https://developer.wordpress.org