LoginSignup
1
1

More than 5 years have passed since last update.

WordPressの固定ページで、サイドバーとかに小階層の記事一覧を表示

Last updated at Posted at 2016-09-28

固定ページは管理画面で階層が付けられるが、
サイドバー(サイドカラム)に、親ページタイトルとその小階層ページ一覧を
ナビゲーションとして出したいときに。

スクリーンショット 2016-09-28 10.07.23.png

wp_list_pages()を使います。

sidebar.php
  <div class="plist">
    <ul>
        <?php
        // 固定ページのみ表示
        if ( is_page() ) {

            // 現在のページのIDを$pageに入れておく
            global $post;
            $page = $post->ID;

            // もし現在の固定ページに親ページがあれば
            if ( $post->post_parent ) {
                // 親ページのIDを$pageに入れなおす
                $page = $post->post_parent;
            }
            // 親のタイトルを取得
            $title = get_the_title( $page );

            $args = array(
                'authors' => '',
                'child_of' => $page, // 先ほど入れたID。親ページを指定
                'date_format' => get_option('date_format'),
                'depth' => 0,
                'echo' => 1,
                'exclude' => '',
                'include' => '',
                'link_after' => '',
                'link_before' => '',
                'post_type' => 'page', // 固定ページを指定
                'post_status' => 'publish', // 公開済のものを指定
                'show_date' => '',
                'sort_column' => 'menu_order, post_title',
                'sort_order' => '',
                'title_li' => '<h2>' . esc_html( $title ) . '</h2>', // タイトル表示
                'walker' => new Walker_Page
            );
            // 小階層のページを取得
            wp_list_pages( $args );
        }
        ?>
    </ul>
</div>
1
1
0

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
  3. You can use dark theme
What you can do with signing up
1
1