Farooq Ahmad Farooq Ahmad - 4 months ago 21
PHP Question

Create a individual custom post type taxonomy Archive Page

I'm currently using archive.php as a generic listing for categories, tags, authors, etc. My theme have 4 custom post type and i want each post type have separate archive template.

How to add separate archive template for each custom post type?

I have a code but not working is there i m missing some thing?

add_filter( 'template_include', 'wpsites_cpt_archive_page_template', 99 );

function wpsites_cpt_archive_page_template( $template ) {

if ( is_post_type_archive(array( first-post-type ) ) ) {
$new_template = locate_template( array( 'your-cpt-template.php' ) );
if ( '' != $new_template ) {
return $new_template ;
}
}

return $template;
}

Answer

checkout wordpress template files docs,

you don't need to use conditionals for separating post type template,

you can simply create archive-{your-post-type}.php file to have different archive template for each post type.

for taxonomy;

taxonomy-{taxonomy}-{term}.php
taxonomy-{taxonomy}.php
tag-{slug}.php
tag-{id}.php
category-{slug}.php
category-{ID}.php

Additionally, if your theme supports standard loop hooks like Genesis Framework, You can use conditional to build custom loop depending on Post type.