복붙노트

[WORDPRESS] ALL * 워드 프레스 카테고리는 부모 카테고리의 템플릿을 사용 * 확인

WORDPRESS

ALL * 워드 프레스 카테고리는 부모 카테고리의 템플릿을 사용 * 확인

해결법


  1. 1.

    /**
     * Iterate up current category hierarchy until a template is found.
     * 
     * @link http://stackoverflow.com/a/3120150/247223
     */ 
    function so_3119961_load_cat_parent_template( $template ) {
        if ( basename( $template ) === 'category.php' ) { // No custom template for this specific term, let's find it's parent
            $term = get_queried_object();
    
            while ( $term->parent ) {
                $term = get_category( $term->parent );
    
                if ( ! $term || is_wp_error( $term ) )
                    break; // No valid parent
    
                if ( $_template = locate_template( "category-{$term->slug}.php" ) ) {
                    // Found ya! Let's override $template and get outta here
                    $template = $_template;
                    break;
                }
            }
        }
    
        return $template;
    }
    
    add_filter( 'category_template', 'so_3119961_load_cat_parent_template' );
    

    즉시 템플릿이 발견 될 때까지 부모의 계층 구조를 반복합니다.


  2. 2.내가 계층 적 분류 체계를 위해 같은 일을하는 방법을 궁금 해서요. 죽은 메딕의 대답은 승 / 몇 개조하면 되겠 어도이 경우에 작동하는 것 같다 :

    내가 계층 적 분류 체계를 위해 같은 일을하는 방법을 궁금 해서요. 죽은 메딕의 대답은 승 / 몇 개조하면 되겠 어도이 경우에 작동하는 것 같다 :

    function load_tax_parent_template() {
        global $wp_query;
    
        if (!$wp_query->is_tax)
            return true; // saves a bit of nesting
    
        // get current category object
        $tax = $wp_query->get_queried_object();
    
        // trace back the parent hierarchy and locate a template
        while ($tax && !is_wp_error($tax)) {
            $template = STYLESHEETPATH . "/taxonomy-{$tax->slug}.php";
    
            if (file_exists($template)) {
                load_template($template);
                exit;
            }
    
            $tax = $tax->parent ? get_term($tax->parent, $tax->taxonomy) : false;
        }
    }
    add_action('template_redirect', 'load_tax_parent_template');
    

  3. 3.TEMPLATEPATH 변수는 하위 주제를 작동하지 않을 수 있습니다 - 부모 테마 폴더에 모습을. 대신 사용 STYLESHEETPATH. 예를 들면

    TEMPLATEPATH 변수는 하위 주제를 작동하지 않을 수 있습니다 - 부모 테마 폴더에 모습을. 대신 사용 STYLESHEETPATH. 예를 들면

    $template = STYLESHEETPATH . "/category-{$cat->slug}.php";
    
  4. from https://stackoverflow.com/questions/3119961/make-all-wordpress-categories-use-their-parent-category-template by cc-by-sa and MIT license