skip to Main Content

I’m creating a plugin and adding a custom post type ssd_vehicles. The single page template is working and it’s using the template files I set in the filter ‘single_template’ but the ‘archive_template’ is not working it goes into the condition of is_post_type_archive ( 'ssd_vehicles' ) but it doesn’t use the template file I return to ‘archive_template’

ssd-plugin.php

register_post_type('ssd_vehicles',
   array(
            'labels'      => array(
                'name'          => __('SSD Vehicles', 'textdomain'),

                'singular_name' => __('SSD Vehicle', 'textdomain'),
            ),
                'public'      => true,
                'has_archive' => 'ssd_vehicles',
                'rewrite'     => array( 'slug' => 'ssd_vehicle' ),
                'supports' => array( 'title', 'editor', 'custom-fields' ),
        )

    );

add_filter('single_template', array('MyClass','create_single_template'));
add_filter('archive_template', array('MyClass','create_archive_template'));

public static function create_single_template( $single_template ){
    
    global $post;   
    if ( 'ssd_vehicles' === $post->post_type ) {
        //THIS WORKS AND DISPLAYS THE CORRECT TEMPLATE FILE

        $single_template = dirname( __FILE__ ) . '/templates/single-ssd_vehicles.php';
    }
    return $single_template;
}

public static function create_archive_template( $archive_template ){
    global $post;   
    if ( is_post_type_archive ( 'ssd_vehicles' ) ) {

        //CODE GOES HERE BUT THE THIS TEMPLATE FILE IS NOT BEING USED
        $archive_template = dirname( __FILE__ ) . '/templates/archive-ssd_vehicles.php';
    }
   
    return $archive_template;
}

2

Answers


  1. Chosen as BEST ANSWER

    It seems that elementor is overriding my custom archive template. I tried disabling and enabling plugins and it seem to work but when elementor pro is active it overrides the template.


  2. Shouldn’t calling for a class require a class ? array( 'MyClass', 'create_single_template' );.
    It seems to work on my end.

    add_filter( 'single_template', array( 'MyClass1', 'create_single_template' ) );
    class MyClass1 {
        public static function create_single_template( $single_template ){ 
            if ( is_singular( 'ssd_vehicles' ) ) {
                $single_template = dirname( __FILE__ ) . '/templates/single-ssd_vehicles.php';
            };
            return $single_template;
        }
    };
    
    add_filter( 'archive_template', array( 'MyClass2', 'create_archive_template' ) );
    class MyClass2 {
        public static function create_archive_template( $archive_template ) { 
            if ( is_post_type_archive ( 'ssd_vehicles' ) ) {
                $archive_template = dirname( __FILE__ ) . '/templates/archive-ssd_vehicles.php';
            };
            return $archive_template;
        }
    };
    

    Additionally, to step away from global variable in WordPress, $global $post to retrieve the post_type can be replaced by get_post_type() or is_singular( 'custom_post_type_slug' ). You could also remove the whole class thing and switch to straight functions to make thing easier (as they don’t seems to have a real role right now).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search