I want to use a certain archive template for a bunch of different parent categories. Therefor I try to change the archive template by using the 'archive_template' filter in functions.php by identifying what slug the current category has.

I've tried to retrieve the category slug by using three different methods as you can see below, but none of them seem to work. What's the correct way of retrieving the current category in functions.php?

function use_custom_template($tpl){
    global $post;

    $tryOne = get_the_category( $post->ID );    
    $tryTwo = get_the_category();
    $tryThree = get_queried_object();
  if (is_tax("sport-fritid-parent") {
    $tpl = dirname( __FILE__ ) . '/category-custom-parent.php';
  return $tpl;

add_filter( 'archive_template', 'use_custom_template' ) ;
  • you can't do this by retrieving the current post, because there is no current post yet, the template and the post loop haven't happened yet, if you're trying to show a specific template for a specific archive of a specific category, why not use the built in template heirarchy support? You can always require or get_template_part in that file if you need to share the code across multiple files
    – Tom J Nowell
    Commented Jul 8, 2022 at 15:31

1 Answer 1


get_queried_object is how you retrieve the term for the current category, although you have to be careful to check for this, as it may not be a category archive. It could be a date archive, or a tag archive, etc etc

At a minimum you should include an is_category check:


Reading the docs for is_category also reveals it takes a parameter so not only can you check if it's a category, you can use it to check if it's a specific category.

You should also look into using locate_template to grab the path to the file rather than dirname( __FILE__ ). This is because the current code has no support for child themes or plugin overrides.

see https://developer.wordpress.org/reference/functions/locate_template/

Another solution would have been to use category-sport-fritid-parent.php and then inside that file called get_template_part to pull in a shared template file.

For reference, here is the official documentation for the filter you're trying to use:


And post in the comments of that doc is an example doing exactly what you're asking how to do:


  • But the $tryThree->slug returns nothing. Perhaps it's easier for me to check this in the actual category.php template with a simple if statement, instead of using two separate category templates? Commented Jul 8, 2022 at 15:41
  • Did you try the other 2 suggestions I made? Did is_category not work? Are you sure this is a category archive and not a page template or page builder screen that implements a category archive? And that the slug is exactly sport-fritid-parent? And is this the category taxonomy that comes with WordPress or a custom taxonomy that's been added by yourself or a theme/plugin that has the name category?
    – Tom J Nowell
    Commented Jul 8, 2022 at 15:49
  • I need to retrieve the slug of the currently queried category object, doesn't is_category just simply check if it's a category or not? Commented Jul 8, 2022 at 15:51
  • yes but if you read the doc I linked you'd see it also takes arguments that let you check if it's a specific category
    – Tom J Nowell
    Commented Jul 8, 2022 at 15:52
  • someone posted an example on the official doc for the filter you're using doing exactly that to achieve your goal using a custom taxonomy instead and is_tax: developer.wordpress.org/reference/hooks/type_template/…
    – Tom J Nowell
    Commented Jul 8, 2022 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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