Site logo

Archived topic

I want to show Element (Block, Content Template) into a custom_template(.php)

3 replies · Started by Ashia on January 11, 2023

Viewing posts 1–4 of 4

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.php in GP theme root folder where single.php, page.php available. I copied codes from page.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 the Custom-template1.php also get updated.

Hi 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_content

Further 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.

It's working!

Thanks a lot David, this is 100% what I wanted, You made my day, Thanks.

Awesome - glad to be of help!

This archived topic is closed to new replies.