Skip Navigation

[Résolu] Preselect specific Layout for « pages » created by NextGen Gallery

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: I have Layout A assigned as the Layout for the Pages post type. However, under certain circumstances I would like to preselect Layout B when a Page is being created in wp-admin.

Solution: There's not a filter available to manipulate the selected Layout here, but you could use the WordPress "save_post" hook to override the User's Layout selection when the post is saved.

function override_default_layout( $post_id ) {
    if( get_post_type($post_id) == 'page' && wp_get_post_parent_id( $post_id ) == 1234 ) {
           // this page is a child of the page with ID 1234 so we need to update the _layouts_template postmeta
           update_post_meta( $post_id, '_layouts_template', 'template-slug');
        }
}
add_action( 'save_post', 'override_default_layout', 100 );

Change '1234' to match the numeric ID of the Photos page, and change 'template-slug' to match the slug of the correct Layout. This way, no matter what is selected, the correct Layout will be applied.

Relevant Documentation: https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

This support ticket is created Il y a 6 années et 5 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 réponses, has 2 voix.

Last updated by webD-3 Il y a 6 années et 5 mois.

Assisted by: Christian Cox.

Auteur
Publications
#589566

As part of the "Add Gallery" workflow in NextGen Gallery/NextGen Plus, a user has the ability to "Create new page" (upon creation of the gallery a new Page with shortcode is created.)

To make the workflow as smooth as possible I'd like for my client to be able to simply "Create new page" from within NextGen while creating the gallery. The issue is that I have a custom Layout for galleries, but by clicking "create new page" Layouts is automatically assigning the default Layout I have specified for the generic "Pages" post type.

Is there a way that I can set a separate default Layout for Pages that meet specific conditions? In this case the new page being created is a child of existing parent page "Photos." Could all children of Photos get the Gallery Layout by default? Or is there some other condition that could enable Toolset to apply the desired Layout upon creation of the gallery page?

Please let me know if I have not described this issue clearly.

#589627

The default Layout will always be selected by default in the new Page editor, but you could override that selection using the save_post hook whenever the Page is saved:

function override_default_layout( $post_id ) {
	if( get_post_type($post_id) == 'page' && wp_get_post_parent_id( $post_id ) == 1234 ) {
           // this page is a child of the page with ID 1234 so we need to update the _layouts_template postmeta
           update_post_meta( $post_id, '_layouts_template', 'template-slug');
        }
}
add_action( 'save_post', 'override_default_layout', 100 );

Change '1234' to match the numeric ID of the Photos page, and change 'template-slug' to match the slug of the correct Layout. This way, no matter what is selected, the correct Layout will be applied.

#589642

Christian,

Thank you. I have added the function you provided, created a new gallery via NextGen, and the generated page correctly applied the desired Layout. I appreciate your quick and accurate reply!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.