How to create a custom page template in wordpress ?

All custom page templates must start with the following code. It tells WordPress that the file is a custom page template, and sets the name of the template.

<?php /* Template name: My Custom Page Template */ ?> 

Note: you can change Template name as per your requirement. just put the name which you want at the front of the “Template name:”

File Creating

  • In your code editor, create a new file in the theme directory. Call it ‘my_custom_page.php’.
  • In the first line of the file, insert the following code:
     <?php /* Template name: My First Test Page */ ?>  
  • Open the existing page.php file.
  • Select all of the code and copy it and paste the copied code under the code you just inserted.
  • Save the file and login into your site wordpress admin
  • Edit the page you would like to use to this new page template
  • Under ‘Page Attributes’ (usually found on the right-hand side of the editor), select ‘My First Test Page’ from the ‘Template’ drop-down and update/publish your page.

Create WordPress Custom Post Type Archive PageTemplates

Post Type Templates

Custom Post Type Templates

  • archive-{post_type}.php
  • single-{post_type}.php
archive-{post_type}.php 
If the custom post type were ‘product’, WordPress would look for archive-product.php to display the archive of posts.
single-{post_type}.php 
If the custom post type were ‘product’, WordPress would look for single-product.php to display the single or permalink of the post.

If these files are not available in your theme WordPress will look for archive.php and or single.php respectfully. If they are not present it will default to index.php.

How to Create a Custom Post Types Archive Page in WordPress

If you want to show your custom post types on your category archive pages?

Add this to your functions.php file:

add_filter('pre_get_posts', 'query_post_type');
function query_post_type($query) {
  if(is_category() || is_tag()) {
    $post_type = get_query_var('post_type');
	if($post_type)
	    $post_type = $post_type;
	else
	    $post_type = array('post','myCustomPostTymeName'); // replace myCustomPostTymeName to your custom post type
    $query->set('post_type',$post_type);
	return $query;
    }
}

Now change the variable ‘myCustomPostTymeName’ to the registered name of your custom post type, and Enjoy!