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

时间:2015-07-01 13:56:16

标签: wordpress wordpress-theming

我目前正在研究Wordpress主题,需要自动为子页面和孙子页面设置自定义模板,因此客户无需担心选择要选择的模板。

我已经有的代码是:

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

代码放在page.php中。这对于子页面非常有用,但我需要包含类似“如果孙子应用grandchild.php&#39;”的内容。

非常感谢任何帮助或建议。谢谢= D

1 个答案:

答案 0 :(得分:0)

试试这个......

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

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