How to Create a Custom Page in WordPress?

WordPress allows you to create pages and posts. They differ in that the posts cannot be tree-like and usually have a chronological hierarchy. Pages do not have categories, but can have child pages, i.e. the tree-like structure.

Sometimes you need to create a different page template, so that the output of the content was different from the rest of the pages. Creating a custom page template in WordPress, allows completely change the layout of content on the page.

Way 1: page template file with an arbitrary name and assign it via the admin console

You need to create .php file, for example, my-page-template.php in your theme folder and inside the file started coding beginning from the notes that this file is a custom page template:

 * Template Name: My Custom Page

// code is here

Now when you create a page in the Page Attributes box you can select a template:

page attributes box

Way 2: the page template using the file with specific name (template hierarchy)

This way involves creating a file in your theme folder with specific name: page-{slug-page}.php or page-{ID of page}.php.

To create a template this way, you need to know the slug of the page and create a file in your theme folder. For example, our slug is contacts, then create the theme file page-contacts.php. and fill it with your code (you can copy the contents from the template file page.php and edit it). Now when you visit the contacts page will be shown the new template. Similarly, you can take the ID (even 33) page and create the file page-33.php.

Way 3, advanced: a page template is included using the template_include filter

It is an advanced method, it is more difficult, but it is powerful. Using this method you can specify a template for any page, post, category. Here the examples:

function my_template( $template ) {

	// if the page has the slug contact-us
	// use the conditional tag is_page()
	if( is_page('contact-us') ){
		if ( $new_template = locate_template( array( 'contact-us.php' ) ) )
			return $new_template ;
	return $template;
add_filter('template_include', 'my_template');

This code must be placed in the theme file functions.php or in the plugin.

Making Useful Page Templates

It’s all good, but we want to create something really useful. Good! Here is an example of a custom page template for 404 Error.

 * The custom page template for displaying 404 pages (Not Found)
 * @package WordPress
 * @subpackage My Child Theme
 * Replace My Child Theme to name of your theme
 * Replace 'my-child-theme' to textdomain name of your theme

get_header(); ?>

<div id="primary" class="site-content">
  <div id="content" role="main">

    <article id="post-0" class="post error404 no-results not-found">

      <header class="entry-header">
        <h1 class="entry-title"><?php esc_html_e( 'It looks like nothing was found at this location. Maybe try search?', 'my-child-theme' ); ?></h1>

      <div class="entry-content">
        <div class="archive-search-form"><?php get_search_form(); ?></div>

          <h3><?php esc_html_e( 'Archives by Year', 'my-child-theme' ); ?></h3>
          <ul><?php wp_get_archives('type=yearly'); ?></ul>
          <h2><?php esc_html_e( 'Archives by Subject', 'my-child-theme' ); ?></h2>
          <ul><?php wp_list_categories('title_li='); ?></ul>

      </div><!-- .entry-content -->

    </article><!-- #post-0 -->

  </div><!-- #content -->
</div><!-- #primary -->

<?php get_footer(); ?>

This file should be named 404.php – remember the template hierarchy of WordPress and I wrote above, about the page template using the file with specific name.

You can now use this knowledge wisely and build something useful for your clients and yourself.