Template files help WordPress show your custom content in a nice way. They control how your custom types look on your site.
Template files for custom types in Wordpress
single-{post_type}.php
archive-{post_type}.phpReplace {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.
single-book.php
archive-book.php
single-movie.php
This template shows a single 'book' post with its title, content, and a custom field called 'author'.
<?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();
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.
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
single-{post_type}.php template file in WordPress?Solution
Step 1: Understand the naming convention
The template file namedsingle-{post_type}.phpis designed to show a single post of the custom post type specified by{post_type}.Step 2: Identify the template role
This template controls how one item of that custom type looks when viewed alone on the site.Final Answer:
To display the layout for a single item of a custom post type -> Option CQuick Check:
single-{post_type}.php shows one item [OK]
- Confusing single and archive templates
- Thinking it registers the post type
- Assuming it styles admin area
Solution
Step 1: Recall template naming for archives
WordPress usesarchive-{post_type}.phpto list all posts of a custom post type.Step 2: Match the post type name
For the custom post type 'book', the correct archive template isarchive-book.php.Final Answer:
archive-book.php -> Option AQuick Check:
archive-{post_type}.php lists posts [OK]
- 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
single-movie.php, what will WordPress display when visiting a single movie post URL?Solution
Step 1: Understand template hierarchy for single posts
WordPress looks forsingle-{post_type}.phpfirst when showing a single custom post type item.Step 2: Apply to 'movie' post type
Sincesingle-movie.phpexists, WordPress uses it to display the single movie post.Final Answer:
The content from single-movie.php template -> Option BQuick Check:
single-movie.php used for single movie post [OK]
- Assuming default single.php is always used
- Thinking archive template shows single post
- Expecting 404 if template exists
single-event.php template is not loading when viewing single events. What is the most likely cause?Solution
Step 1: Check template filename and location
The template must be named exactlysingle-event.phpand placed in the active theme folder.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.Step 3: Combine all issues
Any of these mistakes can cause the template not to load, so all are possible causes.Final Answer:
All of the above -> Option AQuick Check:
Filename, permalinks, and public setting all matter [OK]
- Misnaming the template file
- Not flushing permalinks after changes
- Setting post type as non-public
Solution
Step 1: Identify the correct template for archives
To customize the archive page for 'portfolio', usearchive-portfolio.php.Step 2: Filter posts by tag 'featured'
Use thepre_get_postsfilter hook to modify the main query to show only posts tagged 'featured'.Step 3: Understand why other options are incorrect
single-portfolio.phpis for single items,page-portfolio.phpis for pages, and modifyingindex.phpaffects all posts, not just portfolio.Final Answer:
Use archive-portfolio.php and modify the main query with pre_get_posts filter -> Option DQuick Check:
Archive template + query filter = custom filtered list [OK]
- Using single template for archive display
- Hardcoding queries in page templates
- Modifying index.php which is too broad
Ready to prove it?
Pass the test and this pattern moves unknown
