How to Display Custom Post Type in WordPress Theme Easily
To display a
custom post type in your WordPress theme, create a custom template file or use WP_Query in your theme files to fetch and loop through the posts of that type. Then, output the post data inside the loop to show your custom content on the front end.Syntax
Use WP_Query to fetch posts of your custom post type. The main parts are:
'post_type': The slug of your custom post type.'posts_per_page': Number of posts to show.- The Loop: To display each post's content.
php
<?php
$args = array(
'post_type' => 'your_custom_post_type',
'posts_per_page' => 10
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// Display post content here
the_title('<h2>', '</h2>');
the_content();
}
wp_reset_postdata();
} else {
echo 'No posts found.';
}
?>Example
This example shows how to display a custom post type called book in a theme file. It fetches the latest 5 books and displays their titles and excerpts.
php
<?php
$args = array(
'post_type' => 'book',
'posts_per_page' => 5
);
$book_query = new WP_Query($args);
if ($book_query->have_posts()) {
echo '<ul>';
while ($book_query->have_posts()) {
$book_query->the_post();
echo '<li><h3>' . get_the_title() . '</h3>';
echo '<p>' . get_the_excerpt() . '</p></li>';
}
echo '</ul>';
wp_reset_postdata();
} else {
echo '<p>No books found.</p>';
}
?>Output
<ul><li><h3>Book Title 1</h3><p>Excerpt of book 1...</p></li><li><h3>Book Title 2</h3><p>Excerpt of book 2...</p></li><li><h3>Book Title 3</h3><p>Excerpt of book 3...</p></li><li><h3>Book Title 4</h3><p>Excerpt of book 4...</p></li><li><h3>Book Title 5</h3><p>Excerpt of book 5...</p></li></ul>
Common Pitfalls
- Not calling
wp_reset_postdata()after a custom query, which can break other loops. - Using the wrong
post_typeslug or forgetting to register the custom post type. - Trying to display custom post types without creating or editing the correct theme template files.
- Not checking if posts exist before looping, causing errors.
php
<?php // Wrong: Missing wp_reset_postdata() $args = array('post_type' => 'movie'); $query = new WP_Query($args); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); the_title(); } } // Right: Reset post data after loop wp_reset_postdata(); ?>
Quick Reference
Tips to display custom post types in WordPress themes:
- Use
WP_Querywith'post_type'set to your custom type. - Always check
have_posts()before looping. - Call
wp_reset_postdata()after your custom loop. - Create template files like
archive-{post_type}.phporsingle-{post_type}.phpfor automatic theme support. - Use template hierarchy to customize display easily.
Key Takeaways
Use WP_Query with 'post_type' to fetch custom post type posts in your theme.
Always check if posts exist with have_posts() before looping.
Call wp_reset_postdata() after your custom loop to avoid conflicts.
Create custom template files named archive-{post_type}.php or single-{post_type}.php for better theme integration.
Ensure your custom post type is registered correctly before trying to display it.