0
0
Wordpressframework~5 mins

Template files for custom types in Wordpress

Choose your learning style9 modes available
Introduction

Template files help WordPress show your custom content in a nice way. They control how your custom types look on your site.

You made a new content type like 'Books' and want a special page to show each book.
You want a list page that shows all items of your custom type with a unique style.
You want to change how the single item page looks for your custom content.
You want to add custom layouts or features only for your custom type pages.
Syntax
Wordpress
single-{post_type}.php
archive-{post_type}.php

Replace {post_type} with your custom type name, like book.

single-{post_type}.php shows one item, archive-{post_type}.php shows the list of items.

Examples
This template shows one 'book' item with custom layout.
Wordpress
single-book.php
This template shows a list of all 'book' items.
Wordpress
archive-book.php
Shows a single 'movie' custom type item.
Wordpress
single-movie.php
Sample Program

This template shows a single 'book' post with its title, content, and a custom field called 'author'.

Wordpress
<?php
// single-book.php
get_header();
if (have_posts()) :
  while (have_posts()) : the_post(); ?>
    <article>
      <h1><?php the_title(); ?></h1>
      <div><?php the_content(); ?></div>
      <p><strong>Author:</strong> <?php echo get_post_meta(get_the_ID(), 'author', true); ?></p>
    </article>
<?php
  endwhile;
endif;
get_footer();
OutputSuccess
Important Notes

Always name your template files exactly with your custom type name.

If WordPress does not find your custom template, it uses default templates like single.php or archive.php.

Use get_post_meta() to show extra info saved with your custom type.

Summary

Template files control how your custom types look on your site.

Name templates like single-{post_type}.php and archive-{post_type}.php.

They help show single items and lists with your own style.