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

概要

Breadcrumb NavXTのbcn_template_tagsフィルタは、パンくずリストをカスタマイズするためのフックです。このフィルタを使用することで、パンくずリストの出力を変更したり、特定の条件に基づいて要素を追加または削除することができます。特に、以下のような機能を実装する際によく使われます。

  1. パンくずリストのデザインカスタマイズ
  2. ページタイトルの変更
  3. カスタム投稿タイプの追加
  4. 特定のページでのパンくずリストの非表示
  5. 各パーツのリンク先やテキストの変更
  6. 特定の条件に基づく動的な要素の追加

構文

add_filter('bcn_template_tags', 'custom_breadcrumb', 10, 1);

パラメータ

  • $template_tags: デフォルトのパンくずリストテンプレートタグの配列

戻り値

  • 変更されたパンくずリストテンプレートタグの配列

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

  • Breadcrumb NavXTバージョン: 6.6.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_filter('bcn_template_tags', function($tags) {
    $tags['bcn_breadcrumb'] = str_replace('class="bcn-breadcrumb"', 'class="bcn-breadcrumb custom-class"', $tags['bcn_breadcrumb']);
    return $tags;
});

このサンプルコードでは、bcn_breadcrumbのHTMLクラスを変更してカスタムクラスを追加しています。これにより、CSSでのスタイリングが可能になります。

サンプル2: 特定の投稿タイプのパンくずを非表示

add_filter('bcn_template_tags', function($tags) {
    if (is_singular('my_custom_post_type')) {
        $tags['bcn_breadcrumb'] = '';
    }
    return $tags;
});

このコードは、カスタム投稿タイプが表示されている場合にパンくずリストを非表示にします。

サンプル3: パンくずリストにカスタム項目を追加

add_filter('bcn_template_tags', function($tags) {
    $tags['bcn_breadcrumb'] .= '<li class="custom-item">カスタムアイテム</li>';
    return $tags;
});

このサンプルでは、パンくずリストの最後にカスタムアイテムを追加しています。

サンプル4: ページタイトルを動的に変更

add_filter('bcn_template_tags', function($tags) {
    if (is_page('contact')) {
        $tags['bcn_title'] = 'お問い合わせ';
    }
    return $tags;
});

このコードは、特定のページ(「お問い合わせ」ページ)のタイトルを動的に変更します。

サンプル5: デフォルトの出力をカスタマイズ

add_filter('bcn_template_tags', function($tags) {
    $tags['bcn_parents'] = '<span class="new-parent">' . $tags['bcn_parents'] . '</span>';
    return $tags;
});

この例では、親要素の出力をカスタマイズし、追加のHTMLタグを埋め込んでいます。

これらのサンプルコードは、Breadcrumb NavXTプラグインのbcn_template_tagsフィルタを利用してパンくずリストを柔軟にカスタマイズする方法を示しています。

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


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