2015-07-01 132 views
0

我目前正在使用Wordpress主题,并且需要自动将自定义模板设置为子页面和孙页面,因此客户端无需担心选择要选择的模板。将自定义WordPress模板添加到孙子页面

我已经的代码是:

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

的代码放在page.php文件内。这适用于子页面,但我需要包含'如果孙申请grandchild.php'之类的东西。

任何帮助或建议将不胜感激。由于= d

回答

0

尝试......

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

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

+0

感谢这么多的帮助。如果(count(get_post_ancestors($ post-> ID))== 1){ get_template_part('child'){0} {0} {0}我将它与您的示例中的代码组合起来, ); else(count(get_post_ancestors($ post-> ID))> 0){ get_template_part('grandchild'); } else { get_template_part('parent'); } ?>' –

相关问题