WordPressで固定ページに、そのページの属している階層のページの一覧を表示させる方法を紹介します。
スポンサーリンク
固定ページの子ページ一覧を出力
以下のようなイメージで出力をします!
- 子ページ1
- 子ページ2
- 子ページ3
- 子ページ4
コード
WP_queryを使用します。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?php  $page_list = new WP_Query( 	[ 		'post_type' =>'page', 		'nopaging' => -1, 		'post_parent' => get_the_ID(), 		'order' => 'ASC' 	] ); ?> <ul> 	<?php 	// ループスタート 	if ( $page_list->have_posts() ): 		while ( $page_list->have_posts() ) : $page_list->the_post(); ?> 		<li><?php the_title(); ?></li> 		<?php 		endwhile; 	endif; 	wp_reset_query(); 	?> </ul> | 
6行目の「’post_parnt’」で指定したIDの子ページを返します。
なので現在のページが親ページであれば、ページIDを取得することができる「get_the_ID()」を指定すればOKです!
注意点
子ページで「’post_parent’ => get_the_ID()」を使用すると、その子ページの子ページを出力することになってしますので、
子ページの時は、親ページのIDを取得して「’post_parent’」に指定する必要があります。
スポンサーリンク
   スポンサーリンク
   














