概要
bcn_breadcrumb_types
フィルタは、WordPress プラグイン Breadcrumb NavXT において、ブレッドクラムのタイプをカスタマイズするために用いられます。このフィルタは、特定の条件に応じてブレッドクラムの種類を追加、削除、変更する際に使われます。主に以下のような機能を実装する際によく使用されます。
- カスタム投稿タイプに基づくブレッドクラムの設定
- ページテンプレートに応じたブレッドクラムのタイプの変更
- 特定のカテゴリやタグに関連するブレッドクラムの追加
- サイトの多言語構造に基づくブレッドクラムのカスタマイズ
- 特定の条件下でのユーザー向けブレッドクラムの調整
- プラグインやテーマの特定の機能にフックするための処理
構文
add_filter( 'bcn_breadcrumb_types', 'your_custom_function' );
パラメータ
$types
: 既存のブレッドクラムタイプを含む配列。
戻り値
- 変更された
$types
配列。
使用可能なバージョン
- Breadcrumb NavXT バージョン: 6.5.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: カスタム投稿タイプのブレッドクラムを追加
function add_custom_post_type_to_breadcrumb( $types ) {
$types['custom_post_type'] = 'Custom Post Type';
return $types;
}
add_filter( 'bcn_breadcrumb_types', 'add_custom_post_type_to_breadcrumb' );
このコードは、カスタム投稿タイプ「Custom Post Type」をブレッドクラムタイプに追加します。
サンプル2: カテゴリに基づくブレッドクラムの変更
function change_breadcrumbs_for_category( $types ) {
if ( is_category() ) {
$types['category'] = 'Category: ' . single_cat_title('', false);
}
return $types;
}
add_filter( 'bcn_breadcrumb_types', 'change_breadcrumbs_for_category' );
このコードは、カテゴリーアーカイブページでブレッドクラムのカテゴリ名を変更します。
サンプル3: 多言語サイト用のブレッドクラムタイプ
function add_multilingual_breadcrumbs( $types ) {
if ( function_exists( 'pll_current_language' ) ) {
$types['language'] = pll_current_language();
}
return $types;
}
add_filter( 'bcn_breadcrumb_types', 'add_multilingual_breadcrumbs' );
このコードは、Polylang プラグインを利用して多言語設定に基づくブレッドクラムタイプを追加します。
サンプル4: 特定条件のユーザー向けにブレッドクラムを変更
function modify_breadcrumbs_for_logged_in_users( $types ) {
if ( is_user_logged_in() ) {
$types['dashboard'] = 'Dashboard';
}
return $types;
}
add_filter( 'bcn_breadcrumb_types', 'modify_breadcrumbs_for_logged_in_users' );
このコードは、ログインユーザー向けに「Dashboard」というブレッドクラムタイプを追加します。
サンプル5: 特定テンプレートに応じたブレッドクラムのカスタマイズ
function custom_template_breadcrumbs( $types ) {
if ( is_page_template( 'template-custom.php' ) ) {
$types['custom_template'] = 'Custom Template Page';
}
return $types;
}
add_filter( 'bcn_breadcrumb_types', 'custom_template_breadcrumbs' );
このコードは、特定のページテンプレートを使用している場合にカスタムブレッドクラムタイプを追加します。
引用元はそれぞれ以下の通りです:
1. https://wordpress.org/plugins/breadcrumb-navxt/
2. https://developer.wordpress.org/reference/hooks/bcn_breadcrumb_types/
3. https://github.com/mtrc/breadcrumb-navxt
4. https://www.fiverr.com/themes/adding-breadcrumbs-to-custom-post-types-in-wordpress
5. https://www.wplift.com/wordpress-breadcrumbs
以上、bcn_breadcrumb_types
フィルタの解説とそのサンプルコードについての説明でした。