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
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.
Shouldn’t calling for a class require a class ?
array( 'MyClass', 'create_single_template' );
.It seems to work on my end.
Additionally, to step away from global variable in WordPress,
$global $post
to retrieve thepost_type
can be replaced byget_post_type()
oris_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).