LoginSignup
3

More than 5 years have passed since last update.

WordPress 表示している固定ページが属する最上位〜全ての子孫ページのリストを表示

Last updated at Posted at 2016-07-02

数が増えるとカスタムメニューで対応しきれないよね。
どうせなら自動で出したいよね。

view-all-family-page-link.php
<?php
// 指定した固定ページが属する先祖・子孫全て表示
// 指定がない場合は表示している固定ページ
function view_all_family_page_link( $post_id = '' ) {

    if ( empty( $post_id ) ) {
        if ( ! is_page() ) {
            return;
        }
        global $post;
        $post_id = $post->ID;
    }

    $ancestor = array_pop( get_post_ancestors( $post_id ) );
    if( ! empty( $ancestor ) ) {
        $parent = $ancestor;
    } else {
        $parent = $post_id;
    }

    $args = array(
        'child_of'     => $parent,
        'echo'         => 0,
        'sort_column'  => 'menu_order, post_title',
        'title_li'     => '', 
    );
    $children = wp_list_pages($args);

    $output = '';
    if ( $children ) {
        $output .=  '<nav id="list_pages-' . $parent . '" class="page-navigation">' . "\n";
        $output .=  "<ul>\n" . $children . "</ul>\n";
        $output .=  "</nav>\n";
    }
    echo $output;
}

wp_list_pages() に渡すパラメーターを変えれば階層のあるカスタム投稿タイプでも対応可能。

マークアップはお好みで

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
3