WPでサイトを作るとき、簡単なブログサイトならウィジェットで事足りるのですが、コーポレートサイト等になるとそうはいきません。
規模が大きくなると、サイドバーで親ページに属する子ページのみの一覧を表示・・・なんてざらです。
なので、実際の案件で使ったことがあるページ一覧の取得方法をメモ。
特定のページに属する子ページ一覧を表示
<?php global $post ; $args = array ( 'post_type' => 'page' , 'sort_order' => 'asc' , 'sort_column' => 'menu_order' , 'child_of' => 616 //親ページのID ); $myposts = get_pages( $args ); foreach ( $myposts as $post ) : setup_postdata( $post );?> <li><a href= "<?php the_permalink(); ?>" title= "<?php the_title() ?>" ><?php the_title() ?></a></li> <?php endforeach ; ?> </ul> |
親ページにいる時は子ページ一覧を、
子ページにいるときは自分の親に属する子ページ一覧を表示
<a href="<?php $permalink = get_permalink( $post ->post_parent); echo $permalink ; ?> " title=" <?php the_title(); ?>"> <?php $parent_title = get_the_title( $post ->post_parent); echo $parent_title ; ?> </a></h3> <?php if ( $post ->post_parent) $children = wp_list_pages( "sort_column=menu_order&title_li=&child_of=" . $post ->post_parent. "&echo=0" ); else $children = wp_list_pages( "sort_column=menu_order&title_li=&child_of=" . $post ->ID. "&echo=0" ); if ( $children ) { ?> <ul> <?php echo $children ; ?> </ul> <?php } ?> |