How to create custom page template in WordPress

Custom page template should be used when you want a different layout on any page. There can be multiple reasons for actual uses in your site created on wordpress. This is simple and can be done by following below steps , you just need simple knowledge about php, html and css.

1. Create template file, let us take the example of creating a template where we can display all the posts from a particular category. Also User should be able to navigate easily to each post of that category.

a) Create a template file and add the below code,




// Display title of page

    ', '' ); ?>
        
    

// query used to retrive the posts of a specific category  and one post per page
 false,
    'paged' => $paged,
    'posts_per_page' => 1,
    'post_type' => 'post',
    'category' => 'Your category',
    'order' => 'ASC',
    'post_status' => 'publish'  
);



// The Query
$query = new WP_Query( $post );


if ( $query->have_posts() ) {   
   
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '';
        echo '';  
        echo '';  
        echo '';
        echo '';    
        echo '';
        echo '';
        echo '';
        echo '';
        echo '';
        echo ''; 
        echo '';         
        echo '';
        echo '';
        echo ''; 
        echo '';
        echo '';  
        echo '';  
        echo '';
        echo '
'; previous_posts_link( '« Previous ' ); echo ''; echo ''; next_posts_link( 'Next »', $query->max_num_pages ); echo '
'; echo '

'; echo the_title(); echo '

'; echo '
'; echo the_content(); echo '
'; echo comments_template(); echo '
'; previous_posts_link( '« Previous ' ); echo ''; echo ''; next_posts_link( 'Next »', $query->max_num_pages ); echo '
'; } } else { // no posts found echo '

No Articles Found

'; } wp_reset_postdata(); ?>

b) Few things to note down which are following,

i. Template name must be defined and same name will appear on page while choosing the template
ii. Be cautious while writing the php code mixed with html code as these have different syntax
iii. Above code is used to display one post per page and previous/Next links appears on header and footer of page

2. Upload above file to your theme, under templates directory
wp-content/themes/(Your theme directory)/templates

3. Now you will be able to use the newly created templates on each page. For this go to admin panel of your WordPress site and create or open your page. Now go to template section on right pane, there you will be able to see the entry of new template named as ‘Category – Content’.

Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *