Bird
Raised Fist0
Wordpressframework~5 mins

Template files for custom types in Wordpress

Choose your learning style10 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.

Practice

(1/5)
1. What is the purpose of a single-{post_type}.php template file in WordPress?
easy
A. To register a new custom post type
B. To list all posts of a custom post type in archive format
C. To display the layout for a single item of a custom post type
D. To style the WordPress admin dashboard

Solution

  1. Step 1: Understand the naming convention

    The template file named single-{post_type}.php is designed to show a single post of the custom post type specified by {post_type}.
  2. Step 2: Identify the template role

    This template controls how one item of that custom type looks when viewed alone on the site.
  3. Final Answer:

    To display the layout for a single item of a custom post type -> Option C
  4. Quick Check:

    single-{post_type}.php shows one item [OK]
Hint: Single templates show one item, archive templates show lists [OK]
Common Mistakes:
  • Confusing single and archive templates
  • Thinking it registers the post type
  • Assuming it styles admin area
2. Which of the following is the correct filename for a template that lists all posts of a custom post type named 'book'?
easy
A. archive-book.php
B. single-book.php
C. page-book.php
D. content-book.php

Solution

  1. Step 1: Recall template naming for archives

    WordPress uses archive-{post_type}.php to list all posts of a custom post type.
  2. Step 2: Match the post type name

    For the custom post type 'book', the correct archive template is archive-book.php.
  3. Final Answer:

    archive-book.php -> Option A
  4. Quick Check:

    archive-{post_type}.php lists posts [OK]
Hint: Archive templates list posts; single templates show one [OK]
Common Mistakes:
  • Using single-book.php for listing
  • Confusing page templates with custom post type templates
  • Using content-book.php which is a partial, not a full template
3. Given a custom post type 'movie' and a template file named single-movie.php, what will WordPress display when visiting a single movie post URL?
medium
A. The default single.php template ignoring the custom type
B. The content from single-movie.php template
C. An archive list of all movies
D. A 404 error page

Solution

  1. Step 1: Understand template hierarchy for single posts

    WordPress looks for single-{post_type}.php first when showing a single custom post type item.
  2. Step 2: Apply to 'movie' post type

    Since single-movie.php exists, WordPress uses it to display the single movie post.
  3. Final Answer:

    The content from single-movie.php template -> Option B
  4. Quick Check:

    single-movie.php used for single movie post [OK]
Hint: WordPress uses single-{post_type}.php if it exists [OK]
Common Mistakes:
  • Assuming default single.php is always used
  • Thinking archive template shows single post
  • Expecting 404 if template exists
4. You created a custom post type 'event' but your single-event.php template is not loading when viewing single events. What is the most likely cause?
medium
A. All of the above
B. The template file is named incorrectly or placed in the wrong folder
C. The post type is not set to public
D. You forgot to flush permalinks after registering the post type

Solution

  1. Step 1: Check template filename and location

    The template must be named exactly single-event.php and placed in the active theme folder.
  2. Step 2: Verify permalink settings and post type visibility

    Flushing permalinks is needed after registering a post type; also, the post type must be public to have front-end templates.
  3. Step 3: Combine all issues

    Any of these mistakes can cause the template not to load, so all are possible causes.
  4. Final Answer:

    All of the above -> Option A
  5. Quick Check:

    Filename, permalinks, and public setting all matter [OK]
Hint: Check filename, permalinks, and public flag if template missing [OK]
Common Mistakes:
  • Misnaming the template file
  • Not flushing permalinks after changes
  • Setting post type as non-public
5. You want to create a custom archive page for a post type 'portfolio' that shows only projects tagged with 'featured'. Which template file and WordPress feature should you use?
hard
A. Modify index.php to filter portfolio posts
B. Create single-portfolio.php and add a custom loop inside it
C. Use page-portfolio.php and hardcode the query for featured projects
D. Use archive-portfolio.php and modify the main query with pre_get_posts filter

Solution

  1. Step 1: Identify the correct template for archives

    To customize the archive page for 'portfolio', use archive-portfolio.php.
  2. Step 2: Filter posts by tag 'featured'

    Use the pre_get_posts filter hook to modify the main query to show only posts tagged 'featured'.
  3. Step 3: Understand why other options are incorrect

    single-portfolio.php is for single items, page-portfolio.php is for pages, and modifying index.php affects all posts, not just portfolio.
  4. Final Answer:

    Use archive-portfolio.php and modify the main query with pre_get_posts filter -> Option D
  5. Quick Check:

    Archive template + query filter = custom filtered list [OK]
Hint: Use archive template + pre_get_posts to filter archives [OK]
Common Mistakes:
  • Using single template for archive display
  • Hardcoding queries in page templates
  • Modifying index.php which is too broad
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard