Target the child pages of a CPT to apple a template to?

Hi guys

This seems to be quite simple, but I can’t work it out oddly. I have parent pages of a CPT, which use one template, I’ve targeted conditions for just single pages of a CPT, and then I need the child pages of that CPT to use another template.

TIA :slight_smile:

Create a taxonomy for CPT and add it for CPT main pages.
Use conditions Terms

1 Like

I’d thought of doing that actually. Is that the only way? Happy to, just seemed cleaner and better to target the parents / children? Reason being as it’s an unnecessary step and if the client created new parent/child pages they would need to remember / know to do that you see.

Hey Rob,

you can create your own template condition for this using the following hooks:

  1. builder/settings/{type}/controls_data
  2. bricks/screen_conditions/scores

Since this isn’t perfectly documented yet I created a small screencast demonstrating its usage.

This is the code from the video:

add_filter( 'builder/settings/template/controls_data', function( $data ) {
    $data['controls']['templateConditions']['fields']['childrenOnly'] = [
        'type' => 'checkbox',
        'label' => esc_html__( 'Children only', 'bricks' ),
        'required' => [ 'main', '=', 'postType' ],
    ];

    return $data;
} );

add_filter( 'bricks/screen_conditions/scores', function( $scores, $condition, $post_id, $preview_type ) {
    if ( is_singular() && $condition['main'] === 'postType' && isset( $condition['childrenOnly'] ) ) {
        if ( has_post_parent( $post_id ) ) {
            $scores[] = 8;
        }
    }

    return $scores;
}, 10, 4 );

Let me know if that helped.

Best,

André

2 Likes

Super handy, thanks buddy!

I was struggling to use a Bricks Query loop also to get this current pages in the parent\child relationship as a side nav. I ended up custom coding it with a code block in the end, given you’ve needed to create this I am guessing I wasn’t missing something in Bricks’ filters to do that?

Andrea, when I am using CPT’s, I am usually working with ARCHIVE and SINGLES templates. Using this setup, it’s very easy to achieve with these two conditions and it’s working perfectly fine.

1 - Condition on CPT Archive Template:

2 - Condition on CPT Singles Template:

Hey Michael,

but how does this setup account for parent > child relationships the author was asking for? :thinking:

Best,

André (without the a) :wink:

1 Like

Hey Andre,

read the initial post again. Yes you are correct, the initial question was different … sorry. He asked for parent pageS, my solution is only if the archive Page is the only parent page.

Best,
Michael

Yeah, not the same thing at all.

André you’re a legend that code works great.

Bricks needs this as standard!

Hey André, unfortunately your screencast is no longer available, so I can not double check how you have set it up. For some reason, this does not work for me. I have placed it inside WP Code box and the button shows up inside the template settings. I have a custom post type with parent and child pages. When the button is turned on, it still displays the template on the parent pages.

Could you please help me? I think I am missing or misunderstanding something…

Hey Brian,

if you can create a screencast or something, demonstrate what you did, what does not work and what exactly you’re trying to achieve I’m happy to have a look.

Best,

André

@aslotta André,

Thanks. I have tried to capture it in a screencast. Please let me know if this is sufficient or not!

EDIT: This time as a proper reply.

Hey Brian,

not exactly sure what’s different on your site but for me this still works as expected (see demo).

If you can provide me with some temporary credentials to your site I’m happy to have a look.

Best,

André

Hey André,

Weird indeed! I will give you a temp access in a couple of mins, on my way home ;p
Quick question though. When do you ‘fire’ the wp code box snippet?

Hi André,

See your personal messages!

Hey André,

I could not really let it go haha. I got it fixed, just by making a new template and copy-paste the content of the none-working template to the new one. Now, it does work. I am completely unsure of why it does not work wit my initial made template, but it is working now! Thanks for the effort and time!

Hey Brian,

awesome. Sometimes all it needs is a fresh start. :slight_smile:

Glad I could help!

Best,

André

Hi André,

I have tested the snippet today and it works great, thanks a lot for that. Please allow me to add two minor additions, that for sure are edge cases (but might be interesting for other users reading this thread).

If this functionality ever becomes a part of Bricks core, it might be worth considering …:

  1. If there are two hierarchical CPT’s where two different templates for CPT-children shall be used, the snippet doesn’t work, cause - as it seems to me - it works for ALL childrens of ALL CPT’s, not only for the one/current CPT.

  2. With the snippet active, the tempalte is applied to ALL children and ALL children’s children. So targeting children’s children with another template doesn’t seem to work.

Best Regards
Michael