概要
bcn_breadcrumb_linked
関数は、Breadcrumb NavXT プラグインにおいて、リンク付きのパンくずリストを生成するために使用される関数です。これにより、ウェブサイト内のページの階層構造を視覚的に表現し、ユーザーがナビゲーションしやすくなります。主に以下のような機能を実装する際に使用されます。
- カスタム投稿タイプのパンくずリスト
- タクソノミーアーカイブのリンク生成
- ホームページへのリンクの追加
- ページ階層の明示化
- SEOの改善
- ユーザーエクスペリエンスの向上
構文
bcn_breadcrumb_linked( $item, $link )
パラメータ
$item
(array): パンくずリストアイテムの詳細情報を含む配列。$link
(string): アイテムに関連するリンクの URL。
戻り値
- string: 生成されたリンク付きパンくずリストの HTML。
プラグインおよびWordPressのバージョン
- Breadcrumb NavXT のバージョン: 6.6.0(2023年)
- WordPress のバージョン: 5.8 以降
この関数のアクションでの使用可能性
アクション | 使用可能性 |
---|---|
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: シンプルなパンくずリストの生成
このコードは、bcn_breadcrumb_linked
を使ってシンプルなパンくずリストを生成します。
$item = array('title' => 'ホーム', 'url' => home_url());
$link = '<a href="' . esc_url($item['url']) . '">' . esc_html($item['title']) . '</a>';
echo bcn_breadcrumb_linked($item, $link);
引用元: https://developer.wordpress.org/reference/functions/esc_url/
サンプルコード2: カスタム投稿タイプのパンくずリスト
カスタム投稿タイプのパンくずリストを生成する例です。
if ( 'my_custom_post_type' == get_post_type() ) {
$item = array('title' => 'カスタム投稿', 'url' => get_post_type_archive_link('my_custom_post_type'));
$link = '<a href="' . esc_url($item['url']) . '">' . esc_html($item['title']) . '</a>';
echo bcn_breadcrumb_linked($item, $link);
}
引用元: https://developer.wordpress.org/reference/functions/get_post_type/
サンプルコード3: カテゴリーへのリンク
特定のカテゴリーに対するリンクを生成する際のサンプルです。
$category = get_queried_object();
$item = array('title' => $category->name, 'url' => get_category_link($category->term_id));
$link = '<a href="' . esc_url($item['url']) . '">' . esc_html($item['title']) . '</a>';
echo bcn_breadcrumb_linked($item, $link);
引用元: https://developer.wordpress.org/reference/functions/get_category_link/
サンプルコード4: タグへのリンク
タグアーカイブにリンクを作成する例です。
$tag = get_queried_object();
$item = array('title' => $tag->name, 'url' => get_tag_link($tag->term_id));
$link = '<a href="' . esc_url($item['url']) . '">' . esc_html($item['title']) . '</a>';
echo bcn_breadcrumb_linked($item, $link);
引用元: https://developer.wordpress.org/reference/functions/get_tag_link/
サンプルコード5: ページ階層の表示
ページの階層を表示する際に使う例です。
$parent_id = wp_get_post_parent_id(get_the_ID());
if ($parent_id) {
$parent_post = get_post($parent_id);
$item = array('title' => $parent_post->post_title, 'url' => get_permalink($parent_id));
$link = '<a href="' . esc_url($item['url']) . '">' . esc_html($item['title']) . '</a>';
echo bcn_breadcrumb_linked($item, $link);
}
引用元: https://developer.wordpress.org/reference/functions/get_post_parent_id/
以上が、bcn_breadcrumb_linked
関数の概要とサンプルコードです。この関数を使用することで、サイト内のナビゲーションを改善し、ユーザーエクスペリエンスを向上させることができます。