Home › Forums › Support › I want to show Element (Block, Content Template) into a custom_template(.php)
- This topic has 3 replies, 2 voices, and was last updated 2 years, 9 months ago by
David.
-
AuthorPosts
-
January 11, 2023 at 9:31 pm #2492301
Ashia
Can someone help me with this following issue I have been facing. I am using GP premium, Generateblocks pro, also with a Generatepress child theme. I have created a template named
custom-template1.phpin GP theme root folder wheresingle.php,page.phpavailable. I copied codes frompage.phpfrom header to footer and saved it in that custom tempalte. When I am using the custom template on some specific url using some codes that is working fine.After all that, I created an Element (Element Type: Block) as Content Template and designed it for only
Custom-template1.php`. I do not know how to apply that Block element I created in Custom-template1.php file.Please help me how I can apply that <strong>Content Template Element</strong> to that
Custom-template1.php, so whenever I update that Element theCustom-template1.phpalso get updated.January 12, 2023 at 3:39 am #2492620David
StaffCustomer SupportHi there,
there are no display rules in elements for selecting a custom page template, there are ways around that, and the simplest one would be to add a custom hook inside your custom template.
Here for example:
<?php /* Template Name: Custom Page */ if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. } get_header(); ?> <div <?php generate_do_attr( 'content' ); ?>> <main <?php generate_do_attr( 'main' ); ?>> <?php /** * generate_before_main_content hook. * * @since 0.1 */ do_action( 'generate_before_main_content' ); if ( generate_has_default_loop() ) { while ( have_posts() ) : the_post(); // replace GP function // generate_do_template_part( 'page' ); // with a custom hook do_action('custom_page_template_content'); endwhile; } /** * generate_after_main_content hook. * * @since 0.1 */ do_action( 'generate_after_main_content' ); ?> </main> </div> <?php /** * generate_after_primary_content_area hook. * * @since 2.0 */ do_action( 'generate_after_primary_content_area' ); generate_construct_sidebars(); get_footer();Its a copy of the GP
page.php, and for my tests i saved it as ‘custom-page.phpnot that it matters, and this will register a Custom Template calledCustom Page`In the code above you will see:
// replace GP function // generate_do_template_part( 'page' ); // with a custom hook do_action('custom_page_template_content');I commented out, but you cab remove the
generate_do_template_part( 'page' )function.
And in its place add your own custom hook eg.do_action('custom_page_template_content');Then in your GP Element, do not use Content Template, use a Block Element – Hook, and in the Hook list select Custom and add the name of your custom hook eg.
custom_page_template_contentFurther Notes: the template, like the GP Page template includes the header and footer callbacks, so there is no need to add them in the template.
January 12, 2023 at 4:16 am #2492657Ashia
It’s working!
Thanks a lot David, this is 100% what I wanted, You made my day, Thanks.
January 12, 2023 at 8:02 am #2493034David
StaffCustomer SupportAwesome – glad to be of help!
-
AuthorPosts
- You must be logged in to reply to this topic.