Site logo

[Resolved] Creating A Custom Page Template

Home Forums Support [Resolved] Creating A Custom Page Template

Home Forums Support Creating A Custom Page Template

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #1874295
    This Is Barry

    Hi team,

    I’m trying to create a custom Page Template without using a child theme.

    I went to the /public_html/wp-content/themes/generatepress folder and created a test.php file, and I put in the following content:

    <?php
    /*
    Template Name: Test
    */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit; // Exit if accessed directly.
    }
    
    get_header(); ?>
    
    	<div id="primary" <?php generate_do_element_classes( 'content' ); ?>>
    		<main id="main" <?php generate_do_element_classes( '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();
    
    					generate_do_template_part( 'page' );
    
    				endwhile;
    			} ?>
    
    <h2>hello world</h2>
    
    <?php
    			/**
    			 * 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();

    When I create a new page and select “Test” as the Template from the drop-down.

    What I get is this:
    https://imgur.com/0WmoqcP

    How can I get my custom portion to appear inside the page container?

    Thanks in advance.

    #1874364
    David
    Staff
    Customer Support

    Hi there,

    first off, DO NOT add files to the Parent Theme or make changes to its files. All of those will be lost when the theme is updated. You have to use a Child Theme:

    https://docs.generatepress.com/article/using-child-theme/

    Then this document explains how to take copies of the theme templates for use in you child themes custom post type:

    https://docs.generatepress.com/article/setting-up-a-simple-custom-post-type/

    #1874792
    This Is Barry

    Hi David,

    Thanks for that input.

    In one of my sites, I created my child theme and then created by test.php file in the child theme folder.
    The custom content began appearing inside the Page Container.
    (no other changes made – to either functions.php, style.css or snapshot.png – which are the only other files in the child theme folder)

    Then, I created a child theme on my other site as well. And I copied over this test.php file into the child theme folder.
    The custom page content renders outside the Page Container.

    The link you gave was on how to create a custom Post type.
    I’m trying to create a custom Page, aren’t they different?

    thanks.

    #1874869
    This Is Barry

    Found out where I was goofing up.

    I was putting my custom HTML content in the test.php which was the Page Template.
    I made a copy of the content-page.php in my child theme and inserted my custom HTML over there.

    The entire content now appears inside the Page container.

    Question – What if I wanted to use a different name for the “content-page”.
    Let’s say I call my content-page –> content-test.php.

    How do I load this in my test.php Page Template?
    I tried both options below:
    generate_do_template_part( ‘test’ );
    and
    generate_do_template_part( ‘content-test’ );

    Both resulted with nothing on the page.

    Can you please tell me how I can call upon a content-page with a different name?

    #1875435
    This Is Barry

    Hi, I’d appreciate some help here.
    Thanks.

    #1875566
    David
    Staff
    Customer Support

    Ok – so first we register out template and name this single-test.php

    <?php
    /*
    Template Name: Test
    */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit; // Exit if accessed directly.
    }
    
    get_header(); ?>
    
    	<div id="primary" <?php generate_do_element_classes( 'content' ); ?>>
    		<main id="main" <?php generate_do_element_classes( '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();
    
    					get_template_part( 'content', 'test' );
    
    				endwhile;
    			} ?>
    
    <?php
    			/**
    			 * 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();

    This line:

    get_template_part( 'content', 'test' );

    will call our content-test.php

    <?php
    /**
     * The template used for displaying page content in single-test.php
     *
     * @package GeneratePress
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit; // Exit if accessed directly.
    }
    ?>
    
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?> <?php generate_do_microdata( 'article' ); ?>>
    	<div class="inside-article">
    		<?php
    		/**
    		 * generate_before_content hook.
    		 *
    		 * @since 0.1
    		 *
    		 * @hooked generate_featured_page_header_inside_single - 10
    		 */
    		do_action( 'generate_before_content' );
    
    		if ( generate_show_entry_header() ) :
    			?>
    
    			<header class="entry-header">
    				<?php
    				/**
    				 * generate_before_page_title hook.
    				 *
    				 * @since 2.4
    				 */
    				do_action( 'generate_before_page_title' );
    
    				if ( generate_show_title() ) {
    					$params = generate_get_the_title_parameters();
    
    					the_title( $params['before'], $params['after'] );
    				}
    
    				/**
    				 * generate_after_page_title hook.
    				 *
    				 * @since 2.4
    				 */
    				do_action( 'generate_after_page_title' );
    				?>
    			</header>
    
    			<?php
    		endif;
    
    		/**
    		 * generate_after_entry_header hook.
    		 *
    		 * @since 0.1
    		 *
    		 * @hooked generate_post_image - 10
    		 */
    		do_action( 'generate_after_entry_header' );
    
    		$itemprop = '';
    
    		if ( 'microdata' === generate_get_schema_type() ) {
    			$itemprop = ' itemprop="text"';
    		}
    		?>
    		<h2>I am custom content in my page content-test template before the entru content</h2>
    		<div class="entry-content"<?php echo $itemprop; // phpcs:ignore -- No escaping needed. ?>>
    			<?php
    			the_content();
    
    			wp_link_pages(
    				array(
    					'before' => '<div class="page-links">' . __( 'Pages:', 'generatepress' ),
    					'after'  => '</div>',
    				)
    			);
    			?>
    		<h2>I am custom content in my page content-test template within the content</h2>	
    		</div>
    
    		<?php
    		/**
    		 * generate_after_content hook.
    		 *
    		 * @since 0.1
    		 */
    		do_action( 'generate_after_content' );
    		?>
    		
    	</div>
    </article> 

    I added some custom content in a couple of places.
    Note the comment line:
    * The template used for displaying page content in single-test.php

    Where we reference the single-test template

    #1875760
    This Is Barry

    Works like a charm!

    Thank you David!

    #1875764
    David
    Staff
    Customer Support

    Sorry it took a while and i misread the Page Template requirement. I had to do a quick test to make sure it worked lol

    Glad to be of help

    #1875971
    This Is Barry

    No problemo. I had my page up and running in no time.
    Glad you told me to avoid messing around in the Parent Theme. Phew!

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