The single-{post-type}.php template in WordPress allows you to create a custom layout for individual posts of a specific custom post type. Here’s a step-by-step guide on how to add and use it:
1. Create Your Custom Post Type (If You Haven’t Already):
- If you haven’t already created your custom post type, you’ll need to do that first. You can use plugins like Custom Post Type UI or write code in your theme’s
functions.phpfile or a custom plugin. - Example (using
functions.php):
PHP
function create_book_post_type() {
$labels = array(
'name' => 'Books',
'singular_name' => 'Book',
'menu_name' => 'Books',
'add_new' => 'Add New Book',
'add_new_item' => 'Add New Book',
'edit_item' => 'Edit Book',
'new_item' => 'New Book',
'view_item' => 'View Book',
'search_items' => 'Search Books',
'not_found' => 'No Books found',
'not_found_in_trash' => 'No Books found in Trash',
);
$args = array(
'label' => 'Books',
'description' => 'Books information pages.',
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'rewrite' => array( 'slug' => 'book' ),
);
register_post_type( 'book', $args );
}
add_action( 'init', 'create_book_post_type', 0 );
- In this example, we’ve created a custom post type called “book”.
2. Create the single-{post-type}.php Template File:
- In your theme’s directory, create a new PHP file named
single-book.php(replace “book” with your custom post type’s slug). - This file will define the layout for single posts of your “book” post type.
3. Add Content to Your Template:
- Open
single-book.phpin a text editor and add the necessary WordPress template tags to display your post’s content. - Example
single-book.php:
PHP
<?php
/**
* The template for displaying single book posts.
*
* @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post
*
* @package Your_Theme_Name
*/
get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main">
<?php
while ( have_posts() ) :
the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
</header><div class="entry-content">
<?php
the_content();
// Display custom fields
if (get_post_meta( get_the_ID(), 'author', true )){
echo "<p>Author: ".get_post_meta( get_the_ID(), 'author', true )."</p>";
}
if (get_post_meta( get_the_ID(), 'isbn', true )){
echo "<p>ISBN: ".get_post_meta( get_the_ID(), 'isbn', true )."</p>";
}
wp_link_pages(
array(
'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'your-theme-text-domain' ),
'after' => '</div>',
)
);
?>
</div><footer class="entry-footer">
<?php
edit_post_link(
sprintf(
wp_kses(
/* translators: %s: Name of current post. Only visible to screen readers */
__( 'Edit <span class="screen-reader-text">%s</span>', 'your-theme-text-domain' ),
array(
'span' => array(
'class' => array(),
),
)
),
wp_kses_post( get_the_title() )
),
'<span class="edit-link">',
'</span>'
);
?>
</footer></article><?php
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
endwhile; // End of the loop.
?>
</main></div><?php
get_sidebar();
get_footer();
- Replace “your-theme-text-domain” with your theme’s text domain.
- Replace “Author” and “ISBN” with the names of your custom fields.
- Customize the HTML and CSS to match your desired layout.
4. View Your Custom Post Type Posts:
- Create or edit posts of your custom post type (“book” in this example).
- View a single post, and you should see the layout defined in your
single-book.phptemplate.
Template Hierarchy:
- WordPress uses a template hierarchy to determine which template file to use.
- For single posts of a custom post type, it will look for:
single-{post-type}.php(e.g.,single-book.php)single.phpsingular.phpindex.php
- If
single-book.phpexists, WordPress will use it. If not, it will fall back to the next template in the hierarchy.
Important Considerations:
- Theme Compatibility: Ensure your custom template works well with your theme’s structure and styling.
- Custom Fields: Use custom fields to add specific data to your custom post types and display them in your template.
- CSS Styling: Add CSS rules to your theme’s stylesheet to style the layout of your custom post type posts.
- Child Themes: If you’re modifying a third-party theme, create a child theme to avoid losing your changes when the parent theme is updated.
Leave a Reply