Alternate post templates
written by: Jeff McNearAlternate post templates
One of the fundamental elements of WordPress theming is the ability to create alternative page templates where at the head of the template file you take the standard entry of:
<?php
/**
* The template for displaying all pages
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages
* and that other 'pages' on your WordPress site may use a
* different template.
*
* @link https://codex.wordpress.org/Template_Hierarchy
*
* @package pkresearch
*/
get_header(); ?>
and modify it so as to specify the alternate template name like this:
<?php
/**
* Template Name:Team Page
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages
* and that other 'pages' on your WordPress site may use a
* different template.
*
* @link https://codex.wordpress.org/Template_Hierarchy
*
* @package pkresearch
*/
get_header(); ?>
adding the crucial line of: Template Name: Team Page
as well as naming that new file something like page-team.php
Once upon a time you would need to use a plugin to accomplish the same thing with posts … but for the past several years that is no longer required!
The methodology is very similar where you change the standard head of single.php that reads:
<?php
/**
* The template for displaying all single posts
*
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
*
* @package 346bg
*/
get_header();
?>
and change it to something like this:
<?php
/**
* The template for displaying all single posts
* Template Name: alternative
* Template Post Type: post
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
*
* @package 346bg
*/
get_header();
?>
Where the crucial entry consists of two additional lines, the first specifying the template name and the second assigning it to a specific post type
* Template Name: alternative
* Template Post Type: post
and naming the new file something like single-alternative.php
The ability to specify the relevant post type(s) also means that custom post types can have multiple templates!
<?php
/**
* The template for displaying all single posts
* Template Name: alternative
* Template Post Type: artwork_type
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
*
* @package 346bg
*/
get_header();
?>
and naming the file something like single-artwork_type-alternative.php
It is worth noting that current developments in Gutenberg and full site editing are introducing templating methodology that can be accomplished via the editor itself