最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can I make Wordpress use a custom template for a child page

programmeradmin1浏览0评论

I have a custom post type called communities. Then I have the appropriate single-community.php page. Works great!

So we want to use some of these "communities" as parent pages. Then underneath them we'd have multiple child pages.

Can I have the parent pages use single-community.php and the child pages use single.php or something similar?

I have a custom post type called communities. Then I have the appropriate single-community.php page. Works great!

So we want to use some of these "communities" as parent pages. Then underneath them we'd have multiple child pages.

Can I have the parent pages use single-community.php and the child pages use single.php or something similar?

Share Improve this question asked May 22, 2014 at 1:19 lz430lz430 1232 silver badges17 bronze badges 1
  • What have you tried so far? Any code you have written, modified or tried? Any research? – Brad Dalton Commented May 22, 2014 at 3:51
Add a comment  | 

1 Answer 1

Reset to default 4

You can filter template_include and replace the single-community.php with a single-child-community.php.

Example

add_filter( 'template_include', function( $template ) {

    if ( ! is_singular() )
        return $template; // not single

    if ( 'communities' !== get_post_type() )
        return $template; // wrong post type

    if ( 0 === get_post()->post_parent )
        return $template; // not a child

    return locate_template( 'single-child-community.php' );
});
发布评论

评论列表(0)

  1. 暂无评论