親子ページのページリストの表示方法を変更

親子ページに子ページのリストを表示していたが、親ページもリンクさせることにした。

親ページにいる場合は、リンクなしで自分自身のタイトルを表示し、子ページにいる場合には親ページのタイトルをリンク付きで表示させた。加えて back to という言葉をタイトルの前に付けるようにした。

表示順序は変更していない。
 sort_column=post_date 日付でソート
 show_date=created ページ投稿日を表示
 sort_order=desc 逆順(最新投稿が最初に表示される)

childpage.php を以下のように変更。

<!-- --------------------------------------------------------------------- -->
<div class="plist">
<?php
//子ページの場合はリンク付きで表示
if($post->post_parent){
  echo 'back to '.'<a href="'.get_permalink($post->post_parent).'">'.get_the_title($post->post_parent).'</a>';
}else{
  echo get_the_title($post->post_parent).' &rsquo;s Childpage';
}
?>
<?php
  if($post->post_parent)
  $children = wp_list_pages("title_li=&sort_column=post_date&show_date=created&sort_order=desc&child_of=".$post->post_parent."&echo=0");
  else
  $children = wp_list_pages("title_li=&sort_column=post_date&show_date=created&sort_order=desc&child_of=".$post->ID."&echo=0");
  if ($children) { ?>
  <?php echo $children; ?>
  <?php } ?>
</div>
<!-- --------------------------------------------------------------------- -->

<!-- --------------------------------------------------------------------- --> <div class="plist"> <?php //子ページの場合はリンク付きで表示 if($post->post_parent){ echo 'back to '.'<a href="'.get_permalink($post->post_parent).'">'.get_the_title($post->post_parent).'</a>'; }else{ echo get_the_title($post->post_parent).' &rsquo;s Childpage'; } ?> <?php if($post->post_parent) $children = wp_list_pages("title_li=&sort_column=post_date&show_date=created&sort_order=desc&child_of=".$post->post_parent."&echo=0"); else $children = wp_list_pages("title_li=&sort_column=post_date&show_date=created&sort_order=desc&child_of=".$post->ID."&echo=0"); if ($children) { ?> <?php echo $children; ?> <?php } ?> </div> <!-- --------------------------------------------------------------------- -->

コメントを残す

モバイルバージョンを終了