0

I want to replace custom taxonomy term template with my custom template. Like using add_filter('single_template','customFun'); replaces single custom post type template.

I have a custom taxonomy e.g CustomCat and there are terms e.g hello cat.

When I view hello-cat I want to have my custom template i.e all code in my plugin like add_filter('single_template');

2

I think you're looking for this dynamic filter within the get_query_template() function:

    /**
     * Filter the path of the queried template by type.
     *
     * The dynamic portion of the hook name, `$type`, refers to the filename -- minus the file
     * extension and any non-alphanumeric characters delimiting words -- of the file to load.
     * This hook also applies to various types of files loaded as part of the Template Hierarchy.
     *
     * Possible values for `$type` include: 'index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date',
     * 'home', 'front_page', 'page', 'paged', 'search', 'single', 'singular', and 'attachment'.
     *
     * @since 1.5.0
     *
     * @param string $template Path to the template. See locate_template().
     */
    return apply_filters( "{$type}_template", $template );

where in your case the type is taxonomy.

You mentioned your own custom plugin. So if you want to override the template for your hello-cat term of your custom taxonomy, with a custom template within your plugin, then you can try the following within your root plugin file:

add_filter( 'taxonomy_template', function( $template )
{
    $mytemplate = __DIR__ . '/templates/custom.php';

    if( is_tax( 'some-custom-tax-slug', 'hello-cat' ) && is_readable( $mytemplate ) )
        $template =  $mytemplate;

    return $template;
} );

where the custom template is placed within your custom plugin folder, like:

/wp-content/plugins/my-plugin/templates/custom.php 
  • first of all thanks for your answer :) but I am still in mid of your solution. Now I am using add_filter( 'taxonomy_template', 'magicalendar_get_event_page' ); function magicalendar_get_event_page($content){ return $content } but I want to run it only when taxonomy is "wordpress" – Bhuvnesh Gupta Feb 23 '16 at 10:13
  • I'm assuming you want to do this only from a plugin, but if modifying the theme is ok, then go with what Pieter Goosen suggests @BhuvneshGupta – birgire Feb 23 '16 at 10:22
  • 2
    is_taxonomy() is depreciated, isn't it ;-) – Pieter Goosen Feb 23 '16 at 10:48
  • 1
    ups, yes, I think I wanted to use is_tax() here, just writing from a sloppy memory, thanks ;-) @PieterGoosen – birgire Feb 23 '16 at 10:55
  • 2
    this happens when there's no caffeine in the morning, but usually happens with caffeinie too, so I've no excuse ;-) @PieterGoosen – birgire Feb 23 '16 at 11:04
2

You really do not need any filter here or any custom template. Just create a taxonomy-{$taxonomy}-{$term->slug}.php template in your child theme (or own theme) where:

  • $taxonomy is the name of your taxonomy

  • $term->slug is the slug of the term.

WordPress will automatically use that template for the particular term when you visit that term's archive page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.