プラグインBreadcrumb NavXTのbcn_breadcrumb_typesフィルタの使用方法・解説

概要

bcn_breadcrumb_types フィルタは、WordPress プラグイン Breadcrumb NavXT において、ブレッドクラムのタイプをカスタマイズするために用いられます。このフィルタは、特定の条件に応じてブレッドクラムの種類を追加、削除、変更する際に使われます。主に以下のような機能を実装する際によく使用されます。

  1. カスタム投稿タイプに基づくブレッドクラムの設定
  2. ページテンプレートに応じたブレッドクラムのタイプの変更
  3. 特定のカテゴリやタグに関連するブレッドクラムの追加
  4. サイトの多言語構造に基づくブレッドクラムのカスタマイズ
  5. 特定の条件下でのユーザー向けブレッドクラムの調整
  6. プラグインやテーマの特定の機能にフックするための処理

構文

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 フィルタの解説とそのサンプルコードについての説明でした。

この関数について質問する


上の計算式の答えを入力してください