Check if a page is a parent or if it's a child page?

后端 未结 4 1378
清酒与你
清酒与你 2021-01-31 09:12

Is it possible to check if a page is a parent or if it\'s a child page?

I have my pages set up like this:

-- Parent

---- Child page 1

---- Child

4条回答
  •  感动是毒
    2021-01-31 09:21

    Put this function in the functions.php file of your theme.

    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
    };
    

    Then you can use it:

    
    

提交回复
热议问题