[Support request] Using an Element for my custom page template

Home Forums Support [Support request] Using an Element for my custom page template

Home Forums Support Using an Element for my custom page template

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #2075861
    Chris

    Hey All,

    I am trying to put together a custom template for a client. We want them to be able to make changes in the Block Editor, so we’re hoping to use an Element for this.

    However, the page we are targeting is a taxonomy, “Car Types”. This taxonomy has custom fields set by ACF to give the taxonomy a lot of additional information.

    When I try to set the location I see there the Custom Post Type “Cars” and the Custom Post Type Taxonomy “Car Types”. When I set the location to the taxonomy it asks me to “Choose…” so I can only apply the layout one car type, not all of them. This instantly loses it’s dynamic appeal since I would have to manually set the location for each car type and set it any time a new car type is added.

    The layout is going to be paragraphs coming from the custom fields onto the page. I don’t need anything else to show, no list of pages that have this “Car Type” term assigned/attached to it.

    So I went ahead and tried to apply the layout by selecting the location to a “Car Type”. I set it to “Classic Cars”. Now the taxonomy page looks the same and didn’t get this layout applied to it. What did happen was the Custom Post that had the “Classic Cars” taxonomy on it, that changed. Which was not my intention.

    To clarify, setting the location to a taxonomy of a CPT will change the CPT page, not the taxonomy page.

    Why?

    In almost every other type of ‘Theme Builder’ you can set the actual taxonomy page but not in GP?

    So now my question is, having built out the custom page template, added it to the child theme, how do I get the Element to apply only to the taxonomy pages? Should I create a custom hook? Apply it via PHP?

    How do you purpose solving this?

    I need to apply an Element to a custom template only and not to any other page on the site. Using a standard GP hook will apply it sitewide — “oh, but just set the location”… you cannot target a CPT taxonomy in GP.

    Please reply where clarification is needed.

    #2076054
    Elvin
    Staff
    Customer Support

    Hi there,

    I need to apply an Element to a custom template only and not to any other page on the site. Using a standard GP hook will apply it sitewide — “oh, but just set the location”… you cannot target a CPT taxonomy in GP.

    You actually can target a CPT taxonomy. The “choose” you see is actually targetting specific taxonomy term.

    But yes I see what you mean with the display rule location lacking an option to target ALL taxonomy terms at once meaning you’ll have to add each term one by one if you set the display rule location through the UI. It is indeed tedious.

    If you’re fine with short PHP snippets, you may find this filter useful –
    https://docs.generatepress.com/article/generate_element_display/

    This comes into play when users find the display rule location options lacking. With this filter, you can apply a custom display rule for the specific block element.

    In your case, let’s say the “Car Type” has a slug of car_type and the block element to be displayed has an ID value for 123

    You can use the filter like this to apply the block to a post with any terms from the taxonomy Car Type.

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        if ( 123 === $element_id && has_term('', 'car_type') ) {
            $display = true;
        }
    
        return $display;
    }, 10, 2 );

    has_term('', 'car_type') basically checks if a post has any terms from the taxonomy car_type.

    Now if you’re trying to apply this on Taxonomy ARCHIVE PAGES then there’s actually a display rule location for that within the UI.

    Example – https://share.getcloudapp.com/KouYqd6D

    But if you want to go all in w/ snippets and reuse the block, something like this should work.

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        if ( 123 === $element_id && has_term('', 'car_type') || is_tax( 'car_type' ); ) {
            $display = true;
        }
    
        return $display;
    }, 10, 2 );
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.