- This topic has 7 replies, 2 voices, and was last updated 7 years ago by Tom.
-
AuthorPosts
-
December 1, 2017 at 4:52 pm #439810Anis
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
December 1, 2017 at 5:17 pm #439816AnisAdditionally, 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
December 1, 2017 at 11:56 pm #439887TomLead DeveloperLead DeveloperHi 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 π
December 2, 2017 at 12:43 pm #440217AnisThanks, 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
December 2, 2017 at 8:34 pm #440333AnisAnd… 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?
π
December 2, 2017 at 10:52 pm #440352TomLead DeveloperLead Developer1. 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 π
December 2, 2017 at 11:15 pm #440361AnisThanks a lot, Tom! It worked… π
Anis
December 3, 2017 at 8:36 am #440595TomLead DeveloperLead DeveloperYou’re welcome π
-
AuthorPosts
- You must be logged in to reply to this topic.