WordPress at a glance

3 Ways of Creating a Page Template

The article shows the ways of creating templates of the WordPress' pages. Each way has its pros and cons. And, before I start, allow me to introduce the meaning of the 'Page' term and its difference from Posts.

The WordPress allows creating both Pages and Posts. The difference is that Posts are shown on front page. Posts have such attribute as one of the Categories of the site. Posts cannot be tree-like, unlike Pages. Pages are applied to such content as
'About Me', 'Contacts', 'Site Map' etc.; and their structure in the site hierarchy is a tree-like. Generally, Posts are used to contain chronological information (meaning that the WP sorts them by publish date, while Pages are used for a tree-like structure that does not depend on the time of publishing. Take a look at this article (post) for example. This article was published as a Post of the 'Codex' category. The links at the page's header lead to the Pages: Functions, Hooks.

Pages are similar to Posts: in fact, they both are located in a single database, and even their content is almost similar: headers, texts, custom fields etc. Both Posts and Pages are actually... 'post' of two different types:

  • Pages are tree-like, so they may have both parental and subsidiary Pages;
  • Posts are arranged and grouped by categories and tags.

WordPress allows creating additional types of Posts: either heirarchical (tree-like), or, well, 'non-heirarchical' ones.

Сreating a page in WordPress

Often, in order to make its information output highly different from the other pages, you need to create a separate page template. When creating a page template in WordPress, you may change the page appearance in a completely different way: delete the header, footer, sidebar. Here on this site, for example, I completely change the page that shows WordPress file code.

Way 1, the classics: creating a page template with a file with a random name, selecting it in Admin Panel

This is the richest way of creating a page template in WordPress. You need to create a .php file (for example, tpl_my-page.php in the theme's folder, and at the beginning of the file you need to write a note that the file is a template of pages:

<?php
/*
Template Name: My page template
*/
?>

<!-- Here is html/php the template code -->

Now, when creating a page in Admin, you may select a template from the Page properties page in Admin:

Starting from WordPress 4.7, you may create such page templates for any type of post - not just for Pages. To do it, add the following string to the commentaries: Template Post Type: post, page, where 'post', 'page' are the names of post types the template belongs to.

/*
Template Name: My page template
Template Post Type: post, page, product
*/

Read more in this article.

Pros:

  • After creation template for one page, you can easily apply it to different pages. For example, you may create a template without the sidebar and use it on different pages.

  • You may return only pages with a selected template. For example, you may return all pages with 'Services' template (services.php file). Sometimes it's very helpful. The template filename is saved in _wp_page_template post meta. Thus, to return all pages with this template, create a query by the post meta (take a look here WP_Query).

Cons:

After creating a template file in a theme folder, you should enter the Admin and install the template for pages. This is not always comfortable during development tasks. So, if you are going to use the template only for one page, use the 2nd way.

How it works:

Entering the page of editing a post of tree-like type on the Admin panel, WordPress looks all template files for the following string:

Template Name: ***

The string may be at any place of the file

All files with the strings are collected and returned to the template selection submenu, to the 'Page attributes' block.

When publishing a page, WP adds the name of the file template into a custom field _wp_page_template (if template is not selected the name sets as default):

_wp_page_template = default
_wp_page_template = tpl_my-page.php

Then, when users visit the page, WordPress checks the _wp_page_template custom field. If the template is installed, it uses the template file; otherwise, WP continues to search the page template by hierarchy.

menu

Way 2: page template by a file with a specified name (or hierarchy of template files)

This method implies creation of a file with a specified name in the theme folder: page-{page-slug}.php, or page-{page-ID}.php. Read more here.

When creating a page, install its slug (alternative name). It's used in the page's URL, and it is changeable:

To create a template using this method you need to know the page's slug and create a file in the theme folder. If your slug (as in the picture) is contacts, then create the page-contacts.php file in the theme and full it with necessary code (you can copy-paste the content from page.php template file and edit it manually).

That's it!

Now, when visiting the page, you should see a new template. The same way you may get the page ID (let's imagine that it is 12) and create page-12.php file.

Pros:

There is no need to enter the Admin panel and install the template file. The template starts working right after creating the file. It's easier for developing.

Cons:

The template is created just for one, selected page. It depends on page's slug: if it is changed, the template will not work. If using ID, there is no dependence on slug; still, it becomes unclear in the theme files - what pages cover the template (if there are some templates with ID).

It is almost useless when creating themes (and, especially, plugins). You may use it when editing your own site, where slug or ID are already known.

How it works:

WordPress select the file to use by the following order (the files should be in the root folder of the theme):

  • {any_name}.php (when page template is used)
  • page-{post_slug}.php
  • page-{post_id}.php
  • page.php
  • singular.php
  • index.php
menu

Way 3, coding: page template through "template_include" filter

This is an advanced method. It is more sophisticated, but, at the same time, it offers a wider collection of options. Using this method, you may apply a template to any page, post, category: any publication on your site (up to a group of any publications). Take a look at the following examples (read comments in the code):

## Filter passes the $template variable: the path to the template file.
## Changing the path, we change the template file.
add_filter( 'template_include', 'my_template' );
function my_template( $template ) {

	# this is an alternative to the second way
	// if the page with 'portfolio' slug, use the 'page-portfolio.php' template file
	// use is_page() conditional tag
	if( is_page('portfolio') ){
		if ( $new_template = locate_template( array( 'page-portfolio.php' ) ) )
			return $new_template ;
	}

	# template for a group of categories
	// this example uses the file 'tpl_special-cats.php' from theme folder, 
	// as a template for categories with ID 9, names 'No category' and "php" slug
	if( is_category( array( 9, 'No category', 'php') ) ){
		return get_stylesheet_directory() . '/tpl_special-cats.php';
	}

	# template for post by ID
	// template file is located in the plugin folder '/my-plugin/site-template.php'
	global $post;
	if( $post->ID == 12 ){
		return wp_normalize_path( WP_PLUGIN_DIR ) . '/my-plugin/site-template.php';
	}

	# template for pages of random type "book"
	// It's expected that 'book-tpl.php' template file is located in theme folder 
	global $post;
	if( $post->post_type == 'book' ){
		return get_stylesheet_directory() . '/book-tpl.php';
	}

	return $template;

}

The code should be placed into functions.php file of the theme (or into a plugin, or any other possible way). As you see in the example, during the procedure of template_include filter, the Conditional Tags already work, and global variables $wp_query, $post, etc are set.

Pros:

  • You may install a template to any page (or a group of pages). Almost complete freedom in action.

  • Ability to create a template when writing a plugin.

Cons:

You need to write some code and connect it in a separate manner (for example, in functions.php of the theme).

How it works:

Read the filter description template_include.

-

Source

No comments
    Hello, !     Log In . Register