Archived topic
How to apply custom 'sections' template to custom post type posts only?
3 replies · Started by Ria on August 22, 2017
Thanks to this https://generatepress.com/forums/topic/apply-sections-to-custom-post-types/ I'm able to apply sections to my custom post type 'Portfolio'.
And, thanks to this: https://generatepress.com/forums/topic/how-to-override-generate-sections-page-template-php-in-theme/ I'm able to use a custom template for my posts/pages that use Sections.
However, the code makes all pages/posts that use Sections use my new custom template. I'd like to instead use my new custom template just for my 'Portfolio' custom post type.
I know I can modify this code to do that, but lack enough PHP knowledge to know exactly what to modify in order to make that happen. I think I need to change global $post; to something else that calls specifically the cpt 'Portfolio' posts, rather than all posts?
Can anyone help?
You could change your code to this:
add_action( 'wp','tu_override_sections_template' );
function generate_override_sections_template(){
if ( 'your_post_type' == get_post_type() ) {
remove_filter( 'template_include', 'generate_sections_page_template' );
}
}
add_filter( 'template_include', 'generate_sections_custom_page_template' );
function generate_sections_custom_page_template( $template ) {
if ( 'your_post_type' !== get_post_type() ) {
return $template;
}
global $post;
$use_sections = ( isset( $post ) ) ? get_post_meta( $post->ID, '_generate_use_sections', TRUE) : '';
if ( isset( $use_sections['use_sections'] ) && 'true' == $use_sections['use_sections'] ) {
$new_template = get_stylesheet_directory() . '/generate-sections-page-template.php';
if ( '' != $new_template ) {
return $new_template;
}
}
return $template;
}
Just update your_post_type to whatever your post type ID is.
Thank you Tom! :)
You're welcome! :)