How to create separate template for parent post items vs children

Just as it says, have a custom post type with parent/child posts, I want a different template for the parents than the children.

Hi Daniel,
The WordPress template hierarchy does not provide for this scenario:

With a little effort, you could write a function that checks whether the post you are currently in is a parent or child post and then work with conditions within your template to display layout A or B.

Something like this should do (generated by ChatGPT):

function is_parent_or_child_post() {
    global $post;

    // Check if the post has ancestors
    $ancestors = get_post_ancestors($post);

    // If the post has ancestors, it's a child post
    if (!empty($ancestors)) {
        return 'child';
    } else {
        // If the post has no ancestors, it's a parent post
        return 'parent';

Then you can compare against parent or child:

Thanks for jumping in.

Familiar with the content strucutres/template hierarchies. And yes this will require conditionals in the template selection process, just not sure how to get them there. Not afraid to code it (10+ years plugin dev).

Are you showing using that for block conditions?

Otherwise I don’t have options to use custom functions as conditions for Template Settings → Conditions, I only get content type options.

Is it possible to extend that with custom or even develop additional rule types with hooks/filters?

Hey, hope I understand you correctly.

This is what I’ve done on my bricks Electrician site.

I have a custom post type with a parent, then nested children.

Parent is a sign-up page, then child page is the “thank-you” page (nested under the parent).

I used one section, and two containers for a simple layout.

I then use {echo:has_post_parent()} == 1 (or 0)

If the post has a parent, it’s a child (1).

Hope that is what you’re looking for. Works very well to simply create 1 template for that post type.

has_post_parent() is a wordpress function. I try to stick as close to WordPress as possible :slight_smile:

1 Like

Ahh so your using a single template for both and changing contents.

Not quite what I was after as both pages are using different layouts entirely. Will keep that in mind though.

What we ended up doing for now is switching from Hierarchial structure to flat, and added a category taxonomy.

Then changed the rewrite rules to use the category slug in the post url like posts/categories.

So now instead of /features/parent-feature/child-feature, we have /features/feature-category/feature-page.

Requires custom rewrite rules as well as filtering post url generation but pretty simple.

Also allows us to create dedicated template for each (cat/ post type)…

Yes, I found that easiest… just 1 template to manage instead of 2.

It can be done on a <section> level or just the <div class="brxe-container"> (the option I did).

If it’s a parent, show parent container…

If child, show child container…

Parent page is form sign-up

Child page is thank-you :slight_smile: