概要
woocommerce_structured_data_type_for_page
フィルタは、WooCommerceにおけるページの構造化データのタイプをカスタマイズするためのフックです。このフィルタを使用すると、特定のページタイプに対するJSON-LD形式の構造化データを変更できます。特に、SEOの向上やリッチスニペットの表示を意図したカスタマイズが可能です。このフィルタは、主に以下のような機能を実装する際に利用されます。
- 商品ページの構造化データタイプの変更
- カテゴリページの構造化データの追加
- 特定の投稿タイプの構造化データの調整
- 検索結果ページにおけるデータタイプの変更
- 表示するページによって異なる構造化データを返す機能の実装
- 構造化データのテストやデバッグを容易にするためのカスタマイズ
構文
apply_filters( 'woocommerce_structured_data_type_for_page', $type, $page_id );
パラメータ
$type
(string) – ページの構造化データタイプ (例: 商品、カテゴリなど)$page_id
(int) – 対象のページのID
戻り値
string
– 変更された構造化データタイプ
使用可能なバージョン
- WooCommerce バージョン: 3.0 以降
- WordPress バージョン: 4.0 以降
この関数のアクションでの使用可能性
アクション | 使用可能性 |
---|---|
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_structured_data_type_for_page', 'custom_product_structured_data', 10, 2 );
function custom_product_structured_data( $type, $page_id ) {
if ( is_product() ) {
return 'CustomProductType';
}
return $type;
}
このコードは、製品ページにアクセスした際、構造化データのタイプを “CustomProductType” に変更します。
サンプル2: カテゴリページにおけるデータタイプの変更
add_filter( 'woocommerce_structured_data_type_for_page', 'custom_category_structured_data', 10, 2 );
function custom_category_structured_data( $type, $page_id ) {
if ( is_product_category() ) {
return 'CustomCategoryType';
}
return $type;
}
このコードは、商品カテゴリーのページで構造化データタイプを “CustomCategoryType” に変更します。
サンプル3: 記事ページでのデータタイプ変更
add_filter( 'woocommerce_structured_data_type_for_page', 'custom_post_structured_data', 10, 2 );
function custom_post_structured_data( $type, $page_id ) {
if ( is_single() && get_post_type( $page_id ) === 'post' ) {
return 'BlogPosting';
}
return $type;
}
このコードは、通常の投稿ページにおいて構造化データのタイプを “BlogPosting” に設定します。
サンプル4: 検索結果ページでの構造化データの調整
add_filter( 'woocommerce_structured_data_type_for_page', 'custom_search_structured_data', 10, 2 );
function custom_search_structured_data( $type, $page_id ) {
if ( is_search() ) {
return 'SearchResultsPage';
}
return $type;
}
このコードは、検索結果ページにアクセスした時、構造化データタイプを “SearchResultsPage” に変更します。
サンプル5: 特定のページに対するデータタイプの調整
add_filter( 'woocommerce_structured_data_type_for_page', 'custom_specific_page_structured_data', 10, 2 );
function custom_specific_page_structured_data( $type, $page_id ) {
if ( $page_id === 123 ) { // ページIDが123のページ
return 'CustomPageType';
}
return $type;
}
このコードは、特定のページ(IDが123)の場合に構造化データのタイプを “CustomPageType” に変更します。
これらのサンプルコードは、特定のコンディションに基づいて構造化データのタイプを動的に変更するための例です。各サンプルは異なるページ状況に応じたカスタマイズを示しています。