概要
bcn_default_hierarchy_type
フィルタは、WordPressのプラグイン「Breadcrumb NavXT」で使用されるフックで、パンくずリストのデフォルトの階層タイプを変更するために利用されます。このフィルタを使うと、特定のタイプの投稿やページに対して異なる階層の設定を適用することができます。これにより、SEO(検索エンジン最適化)の向上や、ユーザーのナビゲーション体験の向上が可能になります。
主に以下のような機能を実装する際によく使われます:
- カスタム投稿タイプに特定の階層を設定
- ページ階層に基づくカスタムパンくずリストの作成
- 特定条件下でのパンくずリストの表示/非表示の制御
- 複数のパンくずナビゲーションの統合
- デフォルトのパンくずのスタイルの変更
- 特定のユーザー権限に基づくパンくずのカスタマイズ
構文
add_filter('bcn_default_hierarchy_type', 'my_custom_hierarchy_type');
パラメータ
$type
: 変更したいデフォルトの階層タイプ。
戻り値
- 変更された階層タイプ。
使用可能なバージョン
- Breadcrumb NavXT: バージョン6.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
add_filter('bcn_default_hierarchy_type', 'set_custom_post_type_hierarchy');
function set_custom_post_type_hierarchy($type) {
if (is_singular('custom_post_type')) {
return 'custom_hierarchy';
}
return $type;
}
このコードは、カスタム投稿タイプが表示されているときに、特定の階層タイプ(custom_hierarchy
)を設定します。
サンプルコード2
add_filter('bcn_default_hierarchy_type', 'modify_breadcrumb_hierarchy');
function modify_breadcrumb_hierarchy($type) {
if (is_page() && !is_front_page()) {
return 'page_hierarchy';
}
return $type;
}
このサンプルは、フロントページ以外のページが表示されている場合に、ページ専用の階層を使用するように設定しています。
サンプルコード3
add_filter('bcn_default_hierarchy_type', 'conditional_hierarchy_type');
function conditional_hierarchy_type($type) {
if (is_category('news')) {
return 'news_hierarchy';
}
return $type;
}
このコードは、news
カテゴリにいるときに特別な階層を設定しています。
サンプルコード4
add_filter('bcn_default_hierarchy_type', 'change_hierarchy_for_custom_taxonomy');
function change_hierarchy_for_custom_taxonomy($type) {
if (is_tax('custom_taxonomy')) {
return 'taxonomy_hierarchy';
}
return $type;
}
このサンプルは、カスタムタクソノミーのアーカイブページで異なる階層タイプを使用するように変更します。
サンプルコード5
add_filter('bcn_default_hierarchy_type', 'set_hierarchy_for_logged_in_users');
function set_hierarchy_for_logged_in_users($type) {
if (is_user_logged_in()) {
return 'logged_in_hierarchy';
}
return $type;
}
このコードは、ユーザーがログインしている場合に特定の階層を設定します。