プラグインBreadcrumb NavXTのbcn_before_fillアクションの使用方法・解説

概要

bcn_before_fill は、WordPressプラグイン「Breadcrumb NavXT」に属するアクションフックです。このフックは、パンくずリストが生成される前に実行されるため、パンくずリストの内容を変更したり、追加のデータを挿入したりするのに役立ちます。主に以下のような機能を実装する際に使われます:

  1. パンくずリストのエントリをカスタマイズする
  2. 特定の条件に基づいてパンくずリストを変更する
  3. ユーザーの役割や権限に応じて異なるパンくずリストを表示する
  4. カスタム投稿タイプに特有の情報を追加する
  5. 特定のカスタムフィールドの値をパンくずリストに表示する
  6. 多言語サイトでのパンくずリスト管理

構文

add_action('bcn_before_fill', 'your_function_name');

パラメータ

  • bcn:Breadcrumb NavXTのインスタンスオブジェクト

戻り値

特に戻り値はありませんが、パンくずリストの状態に影響を与えることができます。

使用可能なプラグインバージョン

  • Breadcrumb NavXTのバージョン:6.4.0以降
  • WordPressのバージョン:5.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_action('bcn_before_fill', 'add_custom_home_breadcrumb');

function add_custom_home_breadcrumb($bcn) {
    $bcn->add(new bcn_breadcrumb('ホーム', home_url()));
}

このコードは、パンくずリストの最初に「ホーム」というエントリを追加しています。

引用元: https://example.com/sample1

サンプルコード2

add_action('bcn_before_fill', 'custom_post_type_breadcrumb');

function custom_post_type_breadcrumb($bcn) {
    if (is_singular('custom_post_type')) {
        $bcn->add(new bcn_breadcrumb('カスタムポスト', get_post_type_archive_link('custom_post_type')));
    }
}

特定のカスタム投稿タイプが表示されている場合に、そのアーカイブページへのリンクをパンくずリストに追加します。

引用元: https://example.com/sample2

サンプルコード3

add_action('bcn_before_fill', 'role_based_breadcrumb');

function role_based_breadcrumb($bcn) {
    if (current_user_can('administrator')) {
        $bcn->add(new bcn_breadcrumb('管理者ページ', admin_url()));
    }
}

管理者ユーザーが閲覧している場合に、特定の管理者用ページへのリンクを追加します。

引用元: https://example.com/sample3

サンプルコード4

add_action('bcn_before_fill', 'add_language_specific_breadcrumb');

function add_language_specific_breadcrumb($bcn) {
    if (defined('ICL_LANGUAGE_CODE') && ICL_LANGUAGE_CODE == 'fr') {
        $bcn->add(new bcn_breadcrumb('Accueil', home_url()));
    }
}

WPML等の多言語プラグインが利用されている場合、フランス語のホームリンクを追加します。

引用元: https://example.com/sample4

サンプルコード5

add_action('bcn_before_fill', 'add_custom_field_to_breadcrumb');

function add_custom_field_to_breadcrumb($bcn) {
    if (is_singular()) {
        $custom_field = get_post_meta(get_the_ID(), 'my_custom_field', true);
        if ($custom_field) {
            $bcn->add(new bcn_breadcrumb($custom_field, ''));
        }
    }
}

投稿に存在するカスタムフィールドの値をパンくずリストに追加します。

引用元: https://example.com/sample5

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


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