Yogesh Chauhan's Blog

Custom Post Type Template Files in WordPress

in WordPress on January 31, 2021

From WordPress version 3.0, we can add custom templates for the single display of posts belonging to custom post types.

From version 3.1, WordPress added support for custom templates for archive displays.

How does Template Hierarchy scan the Custom Post Type template files?

WordPress will work through the template hierarchy and use the template file it comes across first.

For example, if you want to create a custom template file for custom post type “projects”, make a single-projects.php.

single-custom_post_type.php > single.php

๐Ÿ‘‰ If you donโ€™t want to create custom template files, WordPress will use archive.php. single.php and index.php files.

Basically,

  • single posts of a custom post type will use single-{post_type}.php
  • single posts of a custom post type archives will use archive-{post_type}.php

๐Ÿ‘‰ If you donโ€™t have this post type archive page you can pass BLOG_URL?post_type={post_type}

where {post_type} is the $post_type argument of the register_post_type() function.

So for the above example, you could create single-projects.php and archive-projects.php template files for single product posts and their archives.

๐Ÿ‘‰ Alternatively, you can use the is_post_type_archive() function in any template file to check if the query shows an archive page of a given post types(s), and the post_type_archive_title() to display the post type title.

Custom Post Type templates

single-{post-type}.php

The single post template used when a visitor requests a single post from a custom post type.

For example, single-projects.php would be used for displaying single posts from a custom post type named projects.

archive-{post-type}.php

The archive post type template is used when visitors request a custom post type archive.

For example, archive-projects.php would be used for displaying an archive of posts from the custom post type named projects.

The archive.php template file is used if the archive-{post-type}.php is not present.

index.php

The index.php is used if any of the following query template for the custom post type is not present.

  • single-{post-type}.php,
  • single.php,
  • archive-{post-type}.php,
  • archive.php,
  • search.php

You can read more on developers.wordpress.com


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
Did you get an error while displaying Special Characters in JavaScript?JavaScriptHow to check if the page is the home page in WordPress?WordPressHow to modify the latest post array using get_posts() in WordPress?WordPressHow to add Read More Read Less Button using JavaScript?JavaScriptCSS backface-visibility PropertyCSSAccessing and Setting features of JavaScript ObjectsJavaScript