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

后端 未结 4 1202
清酒与你
清酒与你 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:33

    I know this is an old question but I was searching for this same question and couldn't find a clear and simple answer until I came up with this one. My answer doesn't answer his explanation but it answers the main question which is what I was looking for.

    This checks whether a page is a child or a parent and allows you to show, for example a sidebar menu, only on pages that are either a child or a parent and not on pages that do not have a parent nor children.

     $post->ID ) );
       if ( is_page() && ($post->post_parent || count( $children ) > 0  )) : 
    ?>
    

提交回复
热议问题