0

As we know we can build a parent category page directly as by file name category-category_name.php. Is there any such process to create a page for child category page. That I have "Continent" - Parent Category and "Country" - Child Category - I want to create a page of Country with scratch.

4
  • 1
    You mean, category-country.php? – Sally CJ Nov 21 '19 at 11:24
  • from category-country.php we can only create parent category page, i want child or sub category page – Roshan Khapung Nov 21 '19 at 11:26
  • I don't get it. What exactly you mean by "child category page"? What is it for? What are you trying to do? – Sally CJ Nov 21 '19 at 11:33
  • You can create a category archive template for every category, no matter what level. So you could have category-continent.php plus category-country.php plus category-city.php and so forth. – WebElaine Nov 21 '19 at 16:15
0

I don't fully understand what you're asking but here's my guess...

Say you have hierarchical taxonomy (e.g. category) with the following term tree:

  • News (ID: 1)
    • World (ID: 2)
      • Africa (ID: 5)
      • Asia (ID: 6)
      • Europe (ID: 7)
    • Business (ID: 3)
    • Science (ID: 4)

Now you visit /category/news/world/africa/ WordPress will look for the following templates: category-africa.php or category-5.php but neither of those exist so WordPress falls back to category.php or archive.php.

If you're asking "Can WordPress work up the tree to the first existing template?" then no, it cannot. But you can implement this functionality with the following...

/**
 * Recurisively check if a term or term parent has a specific template
 *
 * @param   WP_Term $term
 * @return  ?string
 */
function parent_term_template( WP_Term $term ) {

    /**
     * Find template by pattern: taxonomy_name-term_slug.php || taxonomy_name-term_id.php
     *
     * @link https://developer.wordpress.org/reference/functions/locate_template/
     * @var ?string
     */
    $template = locate_template( "{$term->taxonomy}-{$term->slug}.php" ) ?: locate_template( "{$term->taxonomy}-{$term->term_id}.php" );

    /**
     * If template exists, return it
     */
    if ( $template ) {
        return $template;
    }

    /**
     * Else, check if parent template exists
     */
    else if ( !empty( $term->parent ) ) {
        return parent_term_template( get_term_by( 'id', $term->parent, $term->taxonomy ) );
    }

    /**
     * No template
     */
    return null;
}

/**
 * Filter the path of the current template before including it
 *
 * @link https://developer.wordpress.org/reference/hooks/template_include/
 */
add_filter( 'template_include', function( string $template ) {

    /**
     * Apply to all taxonomies
     */
    if ( is_category() || is_tag() || is_tax() ) {

        /**
         * Get term specific template location
         *
         * @var string
         */
        $found = parent_term_template( get_queried_object() );

        /**
         * If template exists, return it
         */
        if ( $found ) {
            return $found;
        }
    }

    return $template;

} );

This will check if the current view is either a category, tag or custom taxonomy archive page. If so, it takes the queried term and passes it to a recursive function that looks for a matching template up the tree of parents until one is found.

For example, if we're viewing /category/news/world/africa/ and there is no category-africa.php template, this code will look for the direct parent template. In this case: category-world.php and if that doesn't exist it will move to category-news.php and finally if that doesn't exist, it will fall back to the default.

Still I have no idea if this answers your question but it might help somebody!

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.