So I basically need to overwrite my category template in my child theme with the one in my plugin but I can't seem to get it working.
I have checked the files paths and everything and there all correct this seems to be the way of doing it but again Wordpress doesn't seem to be using the template from my plugin.
add_filter( 'taxonomy_template', 'load_new_custom_tax_template');
function load_new_custom_tax_template ($tax_template) {
if (is_category()) {
$tax_template = dirname( __FILE__ ) . '/category.php';
}
return $tax_template;
}
So I basically need to overwrite my category template in my child theme with the one in my plugin but I can't seem to get it working.
I have checked the files paths and everything and there all correct this seems to be the way of doing it but again Wordpress doesn't seem to be using the template from my plugin.
add_filter( 'taxonomy_template', 'load_new_custom_tax_template');
function load_new_custom_tax_template ($tax_template) {
if (is_category()) {
$tax_template = dirname( __FILE__ ) . '/category.php';
}
return $tax_template;
}
I noticed that you're using is_category()
in your function, which means you're targetting the default/core category
taxonomy and not a custom taxonomy, hence you should instead use the category_template
hook. E.g.
add_filter( 'category_template', 'load_new_custom_tax_template');
function load_new_custom_tax_template ($tax_template) {
// I don't check for is_category() because we're using the category_template filter.
return dirname( __FILE__ ) . '/templates/category.php';
}
Alternatively, you could use the template_include
hook which runs after the <type>_template
hook such as the category_template
and taxonomy_template
:
add_filter( 'template_include', 'load_new_custom_tax_template');
function load_new_custom_tax_template ($tax_template) {
if (is_category()) {
$tax_template = dirname( __FILE__ ) . '/templates/category.php';
}
return $tax_template;
}
Note though, other plugins can also override the template, hence you might want to use a lower priority, i.e. a greater number as the 3rd parameter for add_filter()
. E.g. 20 as in add_filter( 'category_template', 'load_new_custom_tax_template', 20)
plugin_dir_path(__FILE__ ) . “category.php”
instead of php’sdirname
. – Ben HartLenn Commented Feb 8, 2022 at 16:24category_template
ortemplate_include
work for you? If not, what solution you used? – Sally CJ Commented Feb 20, 2022 at 8:01