How to overwrite the category template in a plugin

admin2025-01-07  3

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;
}
Share Improve this question asked Feb 8, 2022 at 13:18 Ibrarr KhanIbrarr Khan 332 silver badges7 bronze badges 3
  • If that’s your main plug-in file, and your plugins category.php template is in the same directory try Wordpress plugin_dir_path(__FILE__ ) . “category.php” instead of php’s dirname. – Ben HartLenn Commented Feb 8, 2022 at 16:24
  • @BenHartLenn Yes it is in the main plugin directory, I did try the above but unfortunately, no luck its still not using the template in the plugin. – Ibrarr Khan Commented Feb 8, 2022 at 16:40
  • @IbrarrKhan did using category_template or template_include work for you? If not, what solution you used? – Sally CJ Commented Feb 20, 2022 at 8:01
Add a comment  | 

1 Answer 1

Reset to default 0

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)

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736261903a776.html

最新回复(0)