Custom page template in wordpress

WordPress come with default template  for any of the post, page ,category etc.For example we have template category.php for category and page.php for pages.

Below is method two change default template

1.How to change page template.

Method 1

Create custom page template. For eg : use below code to create page template.Create a php file in your them and put below code to create new template

<?php
/*
Template Name: Full Width Page with No Sidebar
*/
get_header(); ?>
    <div id="primary" class="site-content">
        <div id="content" role="main">
            <?php while ( have_posts() ) : the_post(); ?>
                <?php get_template_part( 'content', 'page' ); ?>
                <?php comments_template( '', true ); ?>
            <?php endwhile; // end of the loop. ?>
        </div><!-- #content -->
    </div><!-- #primary -->
<?php get_footer(); ?>

Edit page and select page template Full Width Page with No Sidebar From right template list.

Method 2 :
Create new template with page id – page-{$id}.php

Method 3:
Create page template with slug name.
If your page have slug name about then create page template with name page-about.php

Page template hirarchy.
Below is template order in which template are called

Custom Template — If the Page has a custom Template assigned, WordPress looks for that file and, if found, uses it.
page-{slug}.php — Else WordPress looks for and, if found, uses a specialized template named with the Page’s slug.
page-{id}.php — Else WordPress looks for and, if found, uses a specialized template named with the Page’s ID.
page.php — Else WordPress looks for and, if found, uses the Theme’s default page template.
index.php — Else WordPress uses a the Theme’s index file.

 

2.How to change category template.

Method 1
category-{$id}.php

Method 2
category-{slug}.php

Category template hirarchy
category-{slug}.php — Else WordPress looks for and, if found, uses a specialized template named with the Page’s slug.
category-{id}.php — Else WordPress looks for and, if found, uses a specialized template named with the category‘s ID.
category.php — Else WordPress looks for and, if found, uses the Theme’s default category template.
index.php — Else WordPress uses a the Theme’s index file.

 

 

If any query please drop below your below your comments.

Cheers