How to create a custom page template in a wordpress theme?

If you want to create a custom page template in your WordPress theme then don’t worry in this article, we will teach you how to create a custom page template in WordPress.

Creating a Custom Page Template

Creating a custom page template is very easy. First, you need to open a text editor. In the blank file add this line of code at the top:

<?php /* Template Name: Custom Page Template */ ?>

Once you have added the code, then go to your theme folder and create a directory templates name and save the file inside this directory, custompagetemplate.php

Now you need to login to your WordPress to create a new page or edit an existing one.

Custom page template in a wordpress theme

Now if you change the template and visit this page, then you will get to see a blank page. That’s because your template is empty and does not tell WordPress what to display.

Don’t worry, we will show you how to easily edit your custom page template.

Your custom page template is like any other theme file in WordPress. You can add any HTML, template tags, or PHP code in this file.

The easiest way to create your custom page is by copying the existing page template provided by the installed theme.

You just need to copy page.php from your theme or just copy paste the below code in your custompagetemplate.php file.

<?php get_header(); ?>

<section class="container-fluid index_page">
    <div class="row">
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div class="page-content-box mx-auto">
        <?php echo the_content();?>
    </div>
    <?php endwhile; else : ?>
    <p><?php _e( 'Sorry, no posts found.' ); ?></p>
    <?php endif; ?>
    </div>
</section>

<?php get_footer();?>

Remember only one thing before paste the above code into your custom template code. You just write a Template Name before the get_header(). Like this

<?php 

/* Template Name: Custom Page Template */ 

get_header(); 

?>

<section class="container-fluid">
    <div class="row">
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div class="page-content-box">
        <?php echo the_content();?>
    </div>
    <?php endwhile; else : ?>
    <p><?php _e( 'Sorry, no posts found.' ); ?></p>
    <?php endif; ?>
    </div>
</section>

<?php get_footer();?>

Now you go to edit your page add content and publish the page.

May I Help You?