简体   繁体   中英

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 im 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;
}

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.

lets say you have custom post types (CTP).. cpt1, cpt2, cpt3 ...

then you just need to create archive templates files respectively like that archive-cpt1.php , archive-cpt2.php , archive-cpt3.php .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM