概要
bcn_breadcrumb_title
は、WordPressの人気プラグイン「Breadcrumb NavXT」に含まれるフィルターフックです。このフィルターを使用すると、パンくずリストのタイトルをカスタマイズできます。特に、以下のような機能を実装する際に利用されます:
- パンくずリストのタイトルに特定の接頭辞や接尾辞を追加
- ページごとに異なるタイトルを設定
- 条件に応じて動的にタイトルを変更
- カスタム投稿タイプのタイトル形式の変更
- 国際化対応のタイトルを実装
- SEO対策のためのタイトル調整
フィルタの概要
- 構文:
add_filter( 'bcn_breadcrumb_title', 'callback_function', 10, 2 );
- パラメータ:
$title
: 現在のタイトル$breadcrumb
: 現在のパンくずリストエレメント
- 戻り値: フィルタリングされたタイトル(文字列)
- 使用可能プラグインバージョン: Breadcrumb NavXT 6.0 以降
- 使用可能WordPressバージョン: 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( 'bcn_breadcrumb_title', function( $title, $breadcrumb ) {
return 'Prefix: ' . $title;
}, 10, 2 );
このコードは、すべてのパンくずリストのタイトルの前に「Prefix: 」という接頭辞を追加します。
引用元: https://example.com/sample-1
サンプル 2: ページIDによるタイトル変更
add_filter( 'bcn_breadcrumb_title', function( $title, $breadcrumb ) {
if ( $breadcrumb->ID === 42 ) {
return 'Custom Title for Page 42';
}
return $title;
}, 10, 2 );
このコードは、IDが42のページに対して特定のカスタムタイトルを設定します。
引用元: https://example.com/sample-2
サンプル 3: カスタム投稿タイプのタイトル変更
add_filter( 'bcn_breadcrumb_title', function( $title, $breadcrumb ) {
if ( $breadcrumb->post_type === 'custom_type' ) {
return 'Custom Type: ' . $title;
}
return $title;
}, 10, 2 );
このコードは、カスタム投稿タイプのリスト項目に「Custom Type: 」という接頭辞を追加します。
引用元: https://example.com/sample-3
サンプル 4: 条件に応じたタイトル変更
add_filter( 'bcn_breadcrumb_title', function( $title, $breadcrumb ) {
if ( is_archive() ) {
return 'Archives - ' . $title;
}
return $title;
}, 10, 2 );
このコードは、アーカイブページにいる場合に「Archives – 」をタイトルの前に追加します。
引用元: https://example.com/sample-4
サンプル 5: タイトルの国際化対応
add_filter( 'bcn_breadcrumb_title', function( $title, $breadcrumb ) {
return __( $title, 'text-domain' );
}, 10, 2 );
このコードは、パンくずリストのタイトルを翻訳可能にするための国際化フィルターを追加します。
引用元: https://example.com/sample-5