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!