35

ページが親ページか子ページかを確認することはできますか?

私のページは次のように設定されています。

- 親

---- 子ページ 1

---- 子ページ 2

親ページなら特定のメニューを、子ページなら別のメニューを表示したい。

以下のようなことができることはわかっていますが、特定のページ ID を含めずにもう少し動的にしたいと考えています。

<?php
if ($post->post_parent == '100') { // if current page is child of page with page ID 100
   // show image X 
}
?>
4

5 に答える 5

77

投稿が次のようなサブページであるかどうかをテストできます:
*( http://codex.wordpress.org/Conditional_Tagsから)*

<?php

global $post;     // if outside the loop

if ( is_page() && $post->post_parent ) {
    // This is a subpage

} else {
    // This is not a subpage
}
?>
于 2012-12-17T15:23:44.203 に答える
6

この関数をテーマの functions.php ファイルに入れます。

function is_page_child($pid) {// $pid = The ID of the page we're looking for pages underneath
  global $post;         // load details about this page
  $anc = get_post_ancestors( $post->ID );
  foreach($anc as $ancestor) {
      if(is_page() && $ancestor == $pid) {
          return true;
      }
  }
  if(is_page()&&(is_page($pid)))
     return true;   // we're at the page or at a sub page
  else
      return false;  // we're elsewhere
};

次に、それを使用できます。

<?php 
    if(is_page_child(100)) {
        // show image X 
    } 
?>
于 2014-11-20T16:18:09.060 に答える