WordPress仅显示在Child&;外孙页

WordPress仅显示在Child&;外孙页,wordpress,Wordpress,我试图只在父页面和任何子页面和孙子页面上显示一个侧边栏小部件,但是用我的代码,它不适用于孙子页面 <?php if (is_page(8) || $post->post_parent=="8") { ?> //Do something <?php } else { ?> //Do something else <?php }?> //做点什么 //做点别的 如果您有任何建议,我们将不胜感激。请先了解父母的祖先:

我试图只在父页面和任何子页面和孙子页面上显示一个侧边栏小部件,但是用我的代码,它不适用于孙子页面

    <?php if (is_page(8) || $post->post_parent=="8") { ?>
    //Do something
<?php } else { ?>
          //Do something else
<?php }?>

//做点什么
//做点别的

如果您有任何建议,我们将不胜感激。

请先了解父母的祖先:

$ancestors = get_post_ancestors($post);
然后询问当前postID是否在该数组中:

if( in_array(8, $ancestors) || is_page(8) || (8 == $post->post_parent)  ) :

   //do your thing

endif;

非常感谢你,马可。非常感谢;这么简单的片段。再次感谢,工作做得很好。