【问题标题】:WordPress only display on Child & Grandchild PagesWordPress 仅显示在子页面和孙页面上
【发布时间】:2013-03-16 02:33:37
【问题描述】:

我试图仅在父页面以及任何子页面和孙页面上显示侧边栏小部件,但使用我拥有的代码,它不适用于孙页面。

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

任何建议将不胜感激,谢谢。

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    先获取parent的祖先:

    $ancestors = get_post_ancestors($post);
    

    然后询问当前 postID 是否在该数组中:

    if( in_array(8, $ancestors) || is_page(8) || (8 == $post->post_parent)  ) :
    
       //do your thing
    
    endif;
    

    【讨论】:

    • 非常感谢马可。非常感谢;还有这么简单的sn-p。再次感谢,完美运行。
    猜你喜欢
    • 2012-08-22
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    • 2014-05-25
    相关资源
    最近更新 更多