Black Friday Sale! Get $20 off GP Premium, $40 off our new Lifetime license, and 45% off license renewals/extensions! Learn more

[Resolved] Creating a specialized template for a specific page that uses sections

Home Forums Support Creating a specialized template for a specific page that uses sections

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #439810
    Anis

    Hi,

    I have to create a specialized template for a specific page that has sections enabled.

    This is because I want to set a cookie on this page.
    eg. setcookie(‘page1’, 1, time() + 60, COOKIEPATH, COOKIE_DOMAIN);

    … And on the following page, I have to check the expiry status of the cookie and then redirect to specific pages using php redirection.
    eg. header(‘Location:https://www.mysite.com/mypage/’);

    (Php redirection has to be the first code on a Php page, just after the first <?php opening, i.e. before <HTML> and all other tags. This is what I understand. So hooks or plugins won’t work.)

    When tested on a site with GP free template installed, it worked. eg. I used a specialized template for the page, like page-mypage.php. I set the cookie as above, and it worked, and was able to use another specialized template for the following page and the redirection worked without any problems.

    But on another site with GP Premium 1.5.5 installed and sections enabled, it didn’t work. After several attempts and hours of troubleshooting, I think the problem might be that when a page uses sections, it uses a different template, right? It does not use the default WP page.php or in my case, the specialized template page-mypage.php, right?

    In that case, how can I create a specialized template for a specific page that uses sections?

    Thanks.

    Anis

    GeneratePress 1.4
    GP Premium 1.5.5
    #439816
    Anis

    Additionally, I tested on the GP Premium-installed site with a page that doesn’t use sections. And of course, it worked!

    So a page that uses sections is the concern…

    I look forward to your resolution with (sample) code soon. Thanks.

    Anis

    #439887
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    That’s correct – Sections uses a custom page template.

    You can see a solution I provided here for changing up the template: https://generatepress.com/forums/topic/how-to-apply-custom-sections-template-to-custom-post-type-posts-only/#post-371715

    You would have to edit some stuff, but the groundwork is there to achieve what you’re after I think.

    Let me know if you need more info 🙂

    #440217
    Anis

    Thanks, Tom for the guidance. But I am pressed on time. The site is planned to be launched this Tuesday. So I request detailed help with code asap.

    Is this the way I should be implementing?

    (1) Create a custom post type. (How to do that? Please help me.)

    (2) Create a custom template that uses sections for the custom post type created in step 1. (I definitely need CODE here! 🙁 )

    Thanks in advance.

    Anis

    #440333
    Anis

    And… Where in the file system do I create a custom sections template? Any pre-existing (or default) template that I can get started with to apply my additions/modifications?

    🙁

    #440352
    Tom
    Lead Developer
    Lead Developer

    1. Copy the file in wp-content/plugins/gp-premium/sections/functions/tempates

    2. Paste this file into your child theme and make any adjustments. Name it generate-sections-page-template.php.

    Then use this function to target the page you want to use the custom template on:

    add_filter( 'template_include', 'tu_sections_custom_page_template', 20 );
    function tu_sections_custom_page_template( $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'] && is_page( 'Your Custom Page' ) ) {	
    		
            $new_template = get_stylesheet_directory() . '/generate-sections-page-template.php';
    		
            if ( '' != $new_template ) {
                return $new_template;
            }
        }
    
        return $template;
    }

    Adjust the Your Custom Page to the name of the page you’re targeting.

    That should do it 🙂

    #440361
    Anis

    Thanks a lot, Tom! It worked… 🙂

    Anis

    #440595
    Tom
    Lead Developer
    Lead Developer
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.