[Resolved] How to apply custom 'sections' template to custom post type posts only?

Home Forums Support [Resolved] How to apply custom 'sections' template to custom post type posts only?

Home Forums Support How to apply custom 'sections' template to custom post type posts only?

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #371544
    Ria

    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?

    #371715
    Tom
    Lead Developer
    Lead Developer

    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.

    #372144
    Ria

    Thank you Tom! ๐Ÿ™‚

    #372237
    Tom
    Lead Developer
    Lead Developer

    You’re welcome! ๐Ÿ™‚

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.