Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a individual custom post type taxonomy Archive Page

Tags:

php

wordpress

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;
}
like image 576
FRQ6692 Avatar asked Dec 05 '25 06:12

FRQ6692


2 Answers

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.

like image 189
silver Avatar answered Dec 07 '25 21:12

silver


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 .

like image 24
Owais Ahmed Avatar answered Dec 07 '25 20:12

Owais Ahmed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!