I'm trying to create category and tags archive template from my plugin that has custom post type 'product'.
I used the below code to display search results in template file located at plugin_dir_url(__FILE__).'/inc/templates/product-search.php'
.
function template_chooser( $template ){
global $post;
$plugin_root_dir = WP_PLUGIN_DIR.'/product-plugin/';
if( is_search() && get_post_type($post) == 'product' ){
return $plugin_root_dir.'/inc/templates/domain-search.php';
} else {
return $template;
}
}
add_filter( 'template_include', 'template_chooser' );
Which loads the search template file perfectly.
But when I try to load the archive-product.php (see below code) file using archive filter archive_template
as mentioned in wordpress codex. It simply loads installed theme's archive template with 'No Posts Found'.
below is the code to load archive_template for categories with post type product
:
function get_custom_post_type_template( $archive_template ) {
global $post;
$plugin_root_dir = WP_PLUGIN_DIR.'/product-plugin/';
if ( is_post_type_archive ( 'product' ) ) {
$archive_template = $plugin_root_dir.'/inc/templates/archive-product.php'
}
return $archive_template;
}
add_filter( 'archive_template', 'get_custom_post_type_template' ) ;
I tried various answers online but result is same. could you please show me a way to create a custom archive template for a custom post type created inside a plugin?
$pdm_root_dir
in your template path? Or is it a typo?