最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - How to create Child Category page from scratch at wordpress?

programmeradmin1浏览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.

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.

Share Improve this question edited Nov 21, 2019 at 14:28 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Nov 21, 2019 at 11:10 Roshan KhapungRoshan Khapung 13 bronze badges 4
  • 1 You mean, category-country.php? – Sally CJ Commented Nov 21, 2019 at 11:24
  • from category-country.php we can only create parent category page, i want child or sub category page – Roshan Khapung Commented Nov 21, 2019 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 Commented Nov 21, 2019 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 Commented Nov 21, 2019 at 16:15
Add a comment  | 

1 Answer 1

Reset to default 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/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/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!

发布评论

评论列表(0)

  1. 暂无评论