Skip to content
REDO Interactive

Create a custom template for a custom post type in WordPress

How to create your own single template (single-{slug}.php) for a custom post type in WordPress and display only what you need: title, image and content.

WordPress

The theme decides how a single custom post type entry looks by default. In this tutorial we show how to create your own template (view) for a custom post type in WordPress, so you control exactly what is displayed and how. At the end, the page has a simple layout: header, entry content, footer.

A separate template lets us add CSS only for this post type and write custom functions without affecting the rest of the site.

The screenshots show the Polish admin interface; field names in the code stay as shown.

What we need

  1. The Custom Post Type UI (CPT UI) plugin. It is free in the WordPress plugin directory and creates a custom post type without writing code.
  2. Access to the theme files (via FTP, SSH, or a code editor in a local environment).
  3. Basic knowledge of PHP and the WordPress loop.

Creating a view (template) for a custom post type in WordPress

Step 1. Create the custom post type

After installing the plugin, we open Add/Edit Post Types and add a new post type. It can be any repeatable set of content the site needs. In our example it is a set of guides with a featured image, a title and a description.

The form for adding a new post type in the CPT UI plugin

We scroll to the bottom of the form and select the options we need: title, editor (description) and featured image. We confirm with the Add Post Type button.

Selecting the features supported by the new post type: title, editor and featured image

A new Poradniki (Guides) tab appears in the WordPress admin menu.

Step 2. Add sample entries

We add a few guides and then display a single entry on the site.

List of added guides in the admin panel

In the example we added three guides with different titles, descriptions and images.

The layout of the Poradnik (Guide) custom post type is currently set by the theme we use: Twenty Twenty.

Default view of a single guide in the Twenty Twenty theme

Step 3. Create the template file

Suppose we do not like the default view. We do not want the bottom navigation, the author or the date. To change this for one post type only, we create a separate PHP file that handles it.

We go to the WordPress themes directory (wp-content/themes) and choose our theme. In the example it is twentytwenty.

The themes directory in the WordPress file structure

Twenty Twenty theme files, including singular.php

In the theme directory we look for single.php or, as in Twenty Twenty, singular.php, and make a copy of it. Which of these files is used depends on the theme. Usually one of them renders the single entry view.

We rename the copy to single-{slug}.php, where {slug} is the post type slug entered in the first field of the CPT UI form. In our case it is single-poradnik.php. WordPress picks this file automatically for entries of this type, according to the template hierarchy.

The copied singular.php file renamed to single-poradnik.php

Note: changes in a parent theme are lost when the theme is updated. On a production site, it is better to add the file to a child theme.

Step 4. Edit the template

We open single-poradnik.php. It contains the code copied from singular.php. This is how the file looks in the Twenty Twenty theme:

<?php
/**
 * The template for displaying single posts and pages.
 */

get_header();
?>

<main id="site-content" role="main">

	<?php
	if ( have_posts() ) {
		while ( have_posts() ) {
			the_post();

			get_template_part( 'template-parts/content', get_post_type() );
		}
	}
	?>

</main><!-- #site-content -->

<?php get_template_part( 'template-parts/footer-menus-widgets' ); ?>

<?php get_footer(); ?>

We want to remove the bottom section with the latest entries and show only the guide content together with the site header and footer. We replace the code with the following:

<?php get_header(); ?>

<main id="site-content" role="main">

	<?php while ( have_posts() ) : the_post(); ?>

		<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
			<h2><?php the_title(); ?></h2>
			<?php the_content(); ?>
			<?php the_post_thumbnail( 'full' ); ?>
		</article>

	<?php endwhile; ?>

</main><!-- #site-content -->

<?php get_footer(); ?>

This gives the layout we wanted: header, custom post type content, footer.

A single guide after changing the template: header, content and footer

Common problems

  • WordPress still uses the old view: check that the file name is exactly single-{slug}.php and that the slug matches the post type slug in CPT UI. Re-saving the permalink settings (Settings, Permalinks) can also help.
  • Blank page: get_header() or the while ( have_posts() ) loop with the_post() is missing.
  • No image or description: check that the post type settings enable the featured image and the editor (the thumbnail and editor supports).

Summary

Creating a template for a custom post type is simple, but the details matter:

  • Name the post type simply, without special characters. The slug becomes part of the template file name.
  • Plan the structure of the custom post type and the fields you need to display an entry correctly. Custom field types that CPT UI does not offer can be added with the Advanced Custom Fields plugin (we describe it in Advanced Custom Fields: custom field types in WordPress).
  • Output the fields inside the while ( have_posts() ) : the_post(); loop. Thanks to the_post(), functions such as the_title(), the_content() and the_post_thumbnail() work on the current entry.

← Insights on online stores, websites and code

Start with a conversation

Describe the situation or book a 30-minute call. You talk to the people who will design and build the solution.

REDO Helper

REDO Helper AI assistant

Thank you, your message has been sent. We will get back to you as soon as we can.

Cookies for visit statistics are set only after you agree. Privacy policy