get_category_template

The timeline below displays how wordpress function get_category_template has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Retrieves path of category template in current or parent template.
 *
 * The hierarchy for this template looks like:
 *
 * 1. category-{slug}.php
 * 2. category-{id}.php
 * 3. category.php
 *
 * An example of this is:
 *
 * 1. category-news.php
 * 2. category-2.php
 * 3. category.php
 *
 * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
 * and {@see '$type_template'} dynamic hooks, where `$type` is 'category'.
 *
 * @since 1.5.0
 * @since 4.7.0 The decoded form of `category-{slug}.php` was added to the top of the
 *              template hierarchy when the category slug contains multibyte characters.
 *
 * @see get_query_template()
 *
 * @return string Full path to category template file.
 */
function get_category_template()
{
    $category = get_queried_object();
    $templates = array();
    if (!empty($category->slug)) {
        $slug_decoded = urldecode($category->slug);
        if ($slug_decoded !== $category->slug) {
            $templates[] = "category-{$slug_decoded}.php";
        }
        $templates[] = "category-{$category->slug}.php";
        $templates[] = "category-{$category->term_id}.php";
    }
    $templates[] = 'category.php';
    return get_query_template('category', $templates);
}

WordPress Version: 4.9

/**
 * Retrieve path of category template in current or parent template.
 *
 * The hierarchy for this template looks like:
 *
 * 1. category-{slug}.php
 * 2. category-{id}.php
 * 3. category.php
 *
 * An example of this is:
 *
 * 1. category-news.php
 * 2. category-2.php
 * 3. category.php
 *
 * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
 * and {@see '$type_template'} dynamic hooks, where `$type` is 'category'.
 *
 * @since 1.5.0
 * @since 4.7.0 The decoded form of `category-{slug}.php` was added to the top of the
 *              template hierarchy when the category slug contains multibyte characters.
 *
 * @see get_query_template()
 *
 * @return string Full path to category template file.
 */
function get_category_template()
{
    $category = get_queried_object();
    $templates = array();
    if (!empty($category->slug)) {
        $slug_decoded = urldecode($category->slug);
        if ($slug_decoded !== $category->slug) {
            $templates[] = "category-{$slug_decoded}.php";
        }
        $templates[] = "category-{$category->slug}.php";
        $templates[] = "category-{$category->term_id}.php";
    }
    $templates[] = 'category.php';
    return get_query_template('category', $templates);
}

WordPress Version: 4.7

/**
 * Retrieve path of category template in current or parent template.
 *
 * The hierarchy for this template looks like:
 *
 * 1. category-{slug}.php
 * 2. category-{id}.php
 * 3. category.php
 *
 * An example of this is:
 *
 * 1. category-news.php
 * 2. category-2.php
 * 3. category.php
 *
 * The template hierarchy is filterable via the {@see 'category_template_hierarchy'} hook.
 * The template path is filterable via the {@see 'category_template'} hook.
 *
 * @since 1.5.0
 * @since 4.7.0 The decoded form of `category-{slug}.php` was added to the top of the
 *              template hierarchy when the category slug contains multibyte characters.
 *
 * @see get_query_template()
 *
 * @return string Full path to category template file.
 */
function get_category_template()
{
    $category = get_queried_object();
    $templates = array();
    if (!empty($category->slug)) {
        $slug_decoded = urldecode($category->slug);
        if ($slug_decoded !== $category->slug) {
            $templates[] = "category-{$slug_decoded}.php";
        }
        $templates[] = "category-{$category->slug}.php";
        $templates[] = "category-{$category->term_id}.php";
    }
    $templates[] = 'category.php';
    return get_query_template('category', $templates);
}

WordPress Version: 4.3

/**
 * Retrieve path of category template in current or parent template.
 *
 * Works by first retrieving the current slug, for example 'category-default.php',
 * and then trying category ID, for example 'category-1.php', and will finally fall
 * back to category.php template, if those files don't exist.
 *
 * The template path is filterable via the dynamic {@see '$type_template'} hook,
 * e.g. 'category_template'.
 *
 * @since 1.5.0
 *
 * @see get_query_template()
 *
 * @return string Full path to category template file.
 */
function get_category_template()
{
    $category = get_queried_object();
    $templates = array();
    if (!empty($category->slug)) {
        $templates[] = "category-{$category->slug}.php";
        $templates[] = "category-{$category->term_id}.php";
    }
    $templates[] = 'category.php';
    return get_query_template('category', $templates);
}

WordPress Version: 3.9

/**
 * Retrieve path of category template in current or parent template.
 *
 * Works by first retrieving the current slug, for example 'category-default.php', and then
 * trying category ID, for example 'category-1.php', and will finally fall back to category.php
 * template, if those files don't exist.
 *
 * The template path is filterable via the 'category_template' hook.
 *
 * @since 1.5.0
 *
 * @see get_query_template()
 *
 * @return string Full path to category template file.
 */
function get_category_template()
{
    $category = get_queried_object();
    $templates = array();
    if (!empty($category->slug)) {
        $templates[] = "category-{$category->slug}.php";
        $templates[] = "category-{$category->term_id}.php";
    }
    $templates[] = 'category.php';
    return get_query_template('category', $templates);
}

WordPress Version: 3.7

/**
 * Retrieve path of category template in current or parent template.
 *
 * Works by first retrieving the current slug, for example 'category-default.php', and then
 * trying category ID, for example 'category-1.php', and will finally fall back to category.php
 * template, if those files don't exist.
 *
 * @since 1.5.0
 * @uses apply_filters() Calls 'category_template' on file path of category template.
 *
 * @return string
 */
function get_category_template()
{
    $category = get_queried_object();
    $templates = array();
    if (!empty($category->slug)) {
        $templates[] = "category-{$category->slug}.php";
        $templates[] = "category-{$category->term_id}.php";
    }
    $templates[] = 'category.php';
    return get_query_template('category', $templates);
}