(+84) 463.28.7979

How to Create a Custom Post Types Archive Page in WordPress


Custom Post Types was one of the awesome features included in WordPress 3.0. One of our users asked us a question on twitter (@wpbeginner), how to create a custom post types archive page. We covered it in our initial article about Custom Post Types, but it wasn’t explained thoroughly. So in this article, we will show you a step by step guide on creating a custom post types archive page in WordPress.

1. Creating a Custom Page Template

First step would be creating a custom page template in WordPress. All we will do is add the following code at the top of our new .php file which we will call (custompt-archives.php).

<?php /* Template Name: Custom Post Type Archive */ ?>

2. Adding the Necessary Template Codes

Next, you will need to add the necessary template codes including but not limited to header, footer, sidebar etc. This area will vary for each site, but for basics it would look like this:

<?php get_header(); ?>
This is where our Loop codes will go in the next step.
<?php get_sidebar(); ?>
<?php get_footer();?>

3. Creating our Custom Archive Loop

The loop is explained in details at the WordPress Codex, and we will be using different aspects of it to create our custom archives loop. Our loop will simply list posts with the necessary information surrounding it. You will be responsible for adding styling around it. Our loop:

<?php
global $query_string;
query_posts($query_string . "post_type=YOUR-CUSTOM-POST-TYPE&post_status=publish&posts_per_page=10");
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>

<?php endwhile;
endif; ?>
<div>
	<div><?php next_posts_link('Previous entries') ?></div>
	<div><?php previous_posts_link('Next entries') ?></div>
</div>
<?php wp_reset_query(); ?>

You can change the number of posts being displayed on the page by changing posts_per_page variable value.

Final Code

Your final code for the custom page should look like this:

<?php /* Template Name: Custom Post Type Archive */
get_header(); ?>

<?php
global $query_string;
query_posts($query_string . "post_type=YOUR-CUSTOM-POST-TYPE&post_status=publish&posts_per_page=10");
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
<?php the_excerpt(); ?>

<?php endwhile;
endif; ?>
<div>
	<div><?php next_posts_link('Previous entries') ?></div>
	<div><?php previous_posts_link('Next entries') ?></div>
</div>
<?php wp_reset_query(); ?>

<?php get_sidebar(); ?>
<?php get_footer();?>

Now upload this file in your theme’s directory. After you have done that, you need to go to your WordPress admin panel, and create a new page. Call it “Your Custom Post Type” Archives (or whatever you like to call it) and change the page template to “Custom Post Type Archive” as shown here. Do not add anything in the page content area, and simply publish the page. You should now have a custom post types archive page in WordPress.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>