카테고리 일괄 등록 - functions.php 이용

워드프레스 작성일: 2025-12-24 수정일: 2025-12-24 10:02
수정
// ===================================
// 카테고리 일괄 등록 (한 번 실행 후 삭제)
// ===================================

function trulia_estate_create_categories() {
    // 이미 실행했으면 스킵
    if (get_option('trulia_categories_created')) {
        return;
    }
    
    $categories = array(
        // 1. 와인 입문
        '와인 입문' => array(
            'slug' => 'wine-beginner',
            'children' => array(
                '와인 기초 상식' => 'wine-basics',
                '품종별 가이드' => 'wine-varieties',
                '와인 라벨 읽는 법' => 'wine-label',
                '와인 보관과 서빙' => 'wine-storage',
            )
        ),
        // 2. Estate Wine 특집
        'Estate Wine 특집' => array(
            'slug' => 'estate-wine',
            'children' => array(
                'Estate Wine이란' => 'what-is-estate-wine',
                '추천 Estate Wine 리뷰' => 'estate-wine-review',
                '와이너리 직송 와인' => 'winery-direct',
                '빈티지별 추천' => 'vintage-picks',
            )
        ),
        // 3. 와인 리뷰
        '와인 리뷰' => array(
            'slug' => 'wine-review',
            'children' => array(
                '가격대별 추천' => 'wine-by-price',
                '마트 와인 리뷰' => 'mart-wine',
                '와인샵 추천 와인' => 'wineshop-picks',
                '이달의 와인' => 'wine-of-month',
            )
        ),
        // 4. 음식 페어링
        '음식 페어링' => array(
            'slug' => 'food-pairing',
            'children' => array(
                '한식과 와인' => 'korean-food-wine',
                '양식과 와인' => 'western-food-wine',
                '치즈 페어링' => 'cheese-pairing',
                '디저트 와인' => 'dessert-wine',
            )
        ),
        // 5. 와이너리 투어
        '와이너리 투어' => array(
            'slug' => 'winery-tour',
            'children' => array(
                '국내 와이너리' => 'korea-winery',
                '프랑스' => 'france-winery',
                '이탈리아' => 'italy-winery',
                '미국' => 'usa-winery',
                '기타 지역' => 'other-winery',
            )
        ),
        // 6. 와인 용어사전
        '와인 용어사전' => array(
            'slug' => 'wine-glossary',
            'children' => array()
        ),
    );
    
    foreach ($categories as $parent_name => $parent_data) {
        // 부모 카테고리 생성
        $parent_term = wp_insert_term(
            $parent_name,
            'category',
            array('slug' => $parent_data['slug'])
        );
        
        // 부모 ID 가져오기
        if (is_wp_error($parent_term)) {
            $parent_id = $parent_term->get_error_data();
        } else {
            $parent_id = $parent_term['term_id'];
        }
        
        // 자식 카테고리 생성
        foreach ($parent_data['children'] as $child_name => $child_slug) {
            wp_insert_term(
                $child_name,
                'category',
                array(
                    'slug' => $child_slug,
                    'parent' => $parent_id
                )
            );
        }
    }
    
    // 실행 완료 표시
    update_option('trulia_categories_created', true);
}
add_action('init', 'trulia_estate_create_categories');
언어: PHP