Skip to content

Instantly share code, notes, and snippets.

@koki-h
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save koki-h/646e0fad0ff446c043f2 to your computer and use it in GitHub Desktop.

Select an option

Save koki-h/646e0fad0ff446c043f2 to your computer and use it in GitHub Desktop.
WordPressで現在ページヘのパンくずリストを作成。引数は再帰呼び出し用なので外部からは引数なしで呼び出すこと。タクソノミーには非対応。
<?php
//パンくずリスト
function the_breadcrumb($url="", $child="") {
if($url == "") {
//現在表示されているページのURLを取得
$url = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
}
if ($url == home_url()) {
//TOPページまで行き着いたらechoする
echo 'TOP&nbsp;&gt;&nbsp;' . $child;
return;
}
//ページタイトルを取得
$id = url_to_postid($url);
$title = "";
if ($id) {
$post = get_post($id);
$title = $post->post_title;
} else {
//post_idがない場合(アーカイブページなど)は記事タイプのラベルをページタイトルにする
$post_type = get_post_type_object(get_post_type());
$title = ($post_type->label);
}
//現在ページへのリンクを作成し、下位ページ/上位ページへのリンクと結合(再帰呼び出し)
if ($child) {
//先頭に" > "を追加
$child = '&nbsp;&gt;&nbsp;' . $child;
}
the_breadcrumb(dirname($url),'<a href="'. $url . '">'. esc_html($title) . '</a>' . $child);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment