To create a custom post type archive page in WordPress, you need to create a new template file for your post type and use the wp_query
class to query and display the posts in your post type.
First, you need to create a new template file for your post type. This template file will be used to display the archive page for your post type. To do this, you need to create a new file in your WordPress theme and name it archive-{post_type}.php
, where {post_type}
is the name of your post type.
For example, if your post type is called books
, you would name your template file archive-books.php
.
Once you have created your template file, you can start writing the code to display your custom post type archive page. This will typically involve using the wp_query
class to query and loop through the posts in your post type, and then using WordPress template tags to display the post data.
Here is an example of how to create a custom post type archive page in WordPress:
<?php
// Set up the arguments for the query
$args = array(
'post_type' => 'books', // The name of your post type
'posts_per_page' => 10 // The number of posts to display
);
// Create a new instance of WP_Query
$query = new WP_Query($args);
// Start the loop
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// Display the post data using template tags
the_title();
the_content();
}
}
// Reset the post data
wp_reset_postdata();
In this example, the wp_query
class is used to query the posts in the books
post type. The posts_per_page
argument is used to specify that only 10 posts should be displayed on the archive page.
The have_posts()
and the_post()
functions are used to loop through the posts in the query. Inside the loop, the the_title()
and the_content()
template tags are used to display the post title and content.
After the loop, the wp_reset_postdata()
function is used to reset the post data. This is important to avoid conflicts with other queries on your WordPress site.
When using custom codes, please make sure you know what you are doing. If not used correctly, your site can break.
I hope this helps! Let me know if you have any questions.