Site logo

Archived topic

Apply page layout element with generate_element_display and Custom fields

3 replies · Started by Dan on October 19, 2021

Viewing posts 1–4 of 4

Hi
I've been looking for a way to make it easier for a site's editor to apply layout elements to specific pages, kind of applying a page template.
I didn't want them to go through the display rules or even access the elements modules of GPP.
I wanted to share the current process I use and if anyone has feedback - I would be happy to hear.
I have created a few page layouts via the elements modules and instead of using the page conditions which are rather static for pages, I am utilizing the generate_element_display filter (documentation: https://docs.generatepress.com/article/generate_element_display/)
I am also adding a custom field drop down so the editor can select which page layout to apply to the page.

This is the function:

//Pages with the background layout
add_filter( 'generate_element_display', function( $display, $element_id ) {
    global $post;
	
    if ( 6992 === $element_id && ( is_page() && get_field('page_layout') == 'bg_page' ) ) {
        $display = true;
    }

    return $display;
}, 10, 2 );

What this does using the `generate_element_display' filter:
1. checks what the current element ID is (in this example - 6992)
2. checks if the editor selected the 'bg_page' option from the custom fields
3. if true, sets the layout of the page to 'bg_page' - which is the 6992 element layout.

Until WP 5.9 comes out with a robust page template editing experience, I still think it has a way to go even when they launch in Dec., this is what I will use being in a sort of hybrid mode between using GPP page elements + GB and not going back to manually creating php page templates.

I'm sure this can be improved in the backend, maybe looping through all the available page templates and the custom field options so a specific function doesn't need to be created for each layout, but it's a start...

Dan

Hi there,

my personal approach is to register a custom taxonomy and use that option for a) setting the Display Rules and b) allowing the user to select which element to apply. See here fore an example:

https://generatepress.com/forums/topic/reducing-the-number-of-element-headers/#post-1942770

It eliminates the need for element ID specific filters and as the added advantage of being a taxonomy you can see what elements are applied where and bulk edit them if needed.

Thanks David!
I'll give that a try.
I would also look at a way to make the taxonomies selection to be a radio button or select box so only one layout can be selected at a time.

Dan

This archived topic is closed to new replies.