0

I am creating a wp custom post type plugin. Which supports the custom meta datas as well.Now my question is i have to create a archive page with custom template file. I have added the appropriate function for archive page and as well as single page. Single.php file work fine whereas archive.php is not working. here is my funcitons

add_filter('template_include', 'testimonial_archive');
function testimonial_archive( $template )
{

$post_types = array('astestimonial');
 if ( is_post_type_archive( $post_types ) && ! file_exists( get_stylesheet_directory() . '/testimonial_archive.php' ) )
    $template = dirname(__FILE__) . '/list-testimonial.php';
 if ( is_singular( $post_types ) && ! file_exists( get_stylesheet_directory() . '/single-testimonial.php' ) )
    $template = dirname(__FILE__) . '/single-testimonial.php';
return $template;

}

1 Answer 1

1

The file testimonial_archive.php is not a valid WP template if you don't define a custom way to use that file. The expected name is cpt-archive.php, in your case astestimonial-archive.php. Also, instead of using file_exists, you should use locate_template() to take adventage of template hierarchy and compatibility with child and parent themes.

add_filter('template_include', 'testimonial_archive');
function testimonial_archive( $template ) {

    $post_types = array('astestimonial');

    if ( is_post_type_archive( $post_types ) && locate_template('atestimonial-archive.php' ) == '' ) {
        $template = dirname(__FILE__) . '/list-testimonial.php';
    }

    if ( is_singular( $post_types ) && locate_template( 'single-testimonial.php' ) == '' ) {
        $template = dirname(__FILE__) . '/single-testimonial.php';
    }

    return $template;

}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.