階層のある固定ページのパンくずリスト生成
探せば便利な関数があるのかも知れないですが,勉強を兼ねて自作してみることにします。
functions.phpなどに以下の関数を記述。
/**
* パンくず配列を取得する
*
* @param $post 固定ページpost
* @return array (URL => タイトル)
*/
function get_topic_path_array($post) {
// 親がいない場合
if (!$post->post_parent) {
// TOPのパス代入
$topic_path = array();
$topic_path['/'] = 'TOP'; // 文言は適宜変更
// 親がいる場合
} else {
// 親までのパス代入
$parent_post = get_post($post->post_parent);
$topic_path = get_topic_path_array($parent_post);
}
// 自身のパス挿入
$topic_path[get_permalink($post->ID)] = $post->post_title;
return $topic_path;
}
使用するときは例えばこんな感じ。
<?php $topic_path_array = get_topic_path_array(get_page(get_the_ID())); ?><?php $i = 0; foreach ($topic_path_array as $href => $name) : ?> <?php if ($i < count($topic_path_array) - 1) : ?> <?php echo ''.$name.''; ?> > <?php else : ?> <?php echo $name; ?> <?php endif; ?> <?php $i++; endforeach; ?>

