php - Create a individual custom post type taxonomy Archive Page

313

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

Answer

Solution:

checkout wordpress template files docs,

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

you can simply createarchive-{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.

579

Answer

Solution:

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 .

People are also looking for solutions to the problem: php - hooks didn't load in codeigniter

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.