将自定义 wordpress 模板添加到孙子页面

Add custom wordpress templete to grandchild pages

我目前正在开发一个 Wordpress 主题,需要自动为子页面和孙页面设置自定义模板,这样客户就不需要担心选择哪个模板了。

我已有的代码是:

<?php
 if( $post->post_parent !== 0 ) {
   get_template_part('child');
  } else {
      get_template_part('parent');
  }
  ?>

代码放在page.php内。这对子页面非常有效,但我需要包含 'if grandchild apply grandchild.php' 之类的内容。

如有任何帮助或建议,我们将不胜感激。谢谢=D

试试这个...

if( count(get_post_ancestors($post->ID)) >= 2 ) {
   enter code here
}

利用http://codex.wordpress.org/Function_Reference/get_post_ancestors