본문 바로가기

wordpress/wordpress기초

워드프레스 - 테마 만들기 - custom taxonomy

반응형

taxonomy

기본 워드프레스의 분류법 

catrgory

post의 기본 taxonomy

미분류

category taxonomy의 term

 

function.php 를 분리한다

 

functions.php

<?php
include 'functions-post-type.php';
include 'functions-taxonomy.php';

 

functions-post-type.php

<?php
function bookstore_register_post_type(){
    register_post_type('book', [
        'has_archive' =>true,
        'labels' => [
            'name'                  => '책',
            'singular_name'         => '책',
            'menu_name'             => '책',
            'name_admin_bar'        => '책',
            'add_new'               => '새 책 추가',
            'add_new_item'          => '새 책을 추가합니다',
            'new_item'              => '새 책',
            'edit_item'             => '책 수정',
            'view_item'             => '책 보기',
            'all_items'             => '책 목록 ',
            'search_items'          => '책 검색',
            'parent_item_colon'     => '상위 책:',
            'not_found'             => '현재 입력한 책이 없습니다.',
            'not_found_in_trash'    => '휴지통에 책이 없습니다',
            // 'featured_image'        => '',
            // 'set_featured_image'    => '',
            // 'remove_featured_image' => '',
            // 'use_featured_image'    => '',
            // 'archives'              => '',
            // 'insert_into_item'      => '',
            // 'uploaded_to_this_item' => '',
            // 'filter_items_list'     => '',
            // 'items_list_navigation' => '',
            // 'items_list'            => '',
        ],
        'public' =>true,
        'menu_position' => 3,
        'menu_icon' => 'dashicons-book'
        // 'menu_icon' => get_template_directory_uri().'/images/open-book.svg'
    ]);
}
add_action('init', 'bookstore_register_post_type');
add_action('wp_enqueue_scripts',function(){
    wp_enqueue_style('mbs-style',get_stylesheet_uri());
});



add_action('after_setup_theme', function(){
    add_theme_support('title-tag');
    add_theme_support( 'automatic-feed-links');
});

 

functions-taxonomy.php

<?php
add_action('init', function(){
    register_taxonomy( 'book_author', 'book');
});

functions-taxonomy.php 에서는 taxonomy를 추가 할 수 있다. 

 

그러면 위와같은 태그란이 추가가 된다. 

 

s

<?php
add_action('init', function(){
    register_taxonomy( 'book_author', 'book', [
        'label' => '저자'
    ]);

    register_taxonomy( 'booK_translator', 'book', [
        'label' => '역자 '
    ]);

    register_taxonomy( 'book_subject', 'book', [
        'label' => '주제'
    ]);
});

 

위와같이 수정해주고 

3번쨰 파라미터에 label을 변경해주면 

 

위와같이 나타나게 된다.

 

새책 추가시 이제 저자, 역자, 주제 모두 수정할 수 있게 된다. 

 

 

베르나르 베르베르의 책 두가지를 추가해주었다.

 

 

설정- 고유주소에 가서 업데이트 후 (변경사항 저장)

 

 

저자 - 태그 목록 - 보기를 눌러주면

 

 

베르나르 베르베르 작가를 기반으로 개미, 심판이 출력되는것을 확인 할 수 있다. 

 

 

taxonomy 설정

taxonomy는 

계층형

카테고리, 주제

비계층형

태그, 저자, 역자 

 

등으로 나누어져있다.

 

show_admin_column: 관리자페이지에 표시가 된다

 

functions-taxonomy.php

<?php
add_action('init', function(){
    register_taxonomy( 'book_author', 'book', [
        'label' => '저자',
        'show_admin_column' => true,
    ]);

    register_taxonomy( 'booK_translator', 'book', [
        'label' => '역자',
        'show_admin_column' => true,
    ]);

    register_taxonomy( 'book_subject', 'book', [
        'label' => '주제',
        'show_admin_column' => true,
    ]);
});

 

show_admin_column을 활용하면 해당 label들을 관리자 페이지에서도 조회가 가능하다 

 

 

 

hirerarchical

계층형 구조로 변형할떄 사용한다 

 

functions-taxonomy.php

<?php
add_action('init', function(){
    register_taxonomy( 'book_author', 'book', [
        'label' => '저자',
        'show_admin_column' => true,
    ]);

    register_taxonomy( 'booK_translator', 'book', [
        'label' => '역자',
        'show_admin_column' => true,
    ]);

    register_taxonomy( 'book_subject', 'book', [
        'label' => '주제',
        'show_admin_column' => true,
        'hierarchical' => true
    ]);
});

 

해당 hirerarchical 을 true로 설정하면 

 

기존과 다르게 상위카테고리를 선택하는 란이 생긴다. 즉 계층형 구조로 변경이 된다. 

 

상위 카테고리를 정하고 주제를 만들게되면

해당과같이 계층적으로 나타나게된다. 

 

 

이제 해당 카테고리추가등을 labels 옵션을 통해 변경시켜줄 것이다. 

 

 

비 계층형이면 앞의 label을 기본값으로 

계층형이면 뒤의 label을 기본값으로 세팅한다. 

 

developer.wordpress.org/reference/functions/get_taxonomy_labels/

functions-taxonomy.php

<?php
add_action('init', function () {
    register_taxonomy('book_author', 'book', [
        'labels' => [
            'name'              =>  '저자',
            'singular_name'     =>  '저자',
            'search_items'      =>  '저자 검색',
            'all_items'         =>  '전체 저자',
            'edit_item'         =>  '저자 편집',
            'update_item'       =>  '저자 수정',
            'add_new_item'      =>  '새 저자',
            'new_item_name'     =>  '새 저자 이름',
            'menu_name'         =>  '저자',
            'popular_items' => '많이 사용한 저자',
            'separate_items_with_commas' => '저자를 쉼표로 구분해 주세요',
            'add_or_remove_items' => '저자 추가 제거',
            'choose_from_most_uses' => '가장 많이 사용한 저자중에 고르기'
        ],
        'show_admin_column' => true,
    ]);

    register_taxonomy('booK_translator', 'book', [
        'labels' => [
            'name'              =>  '역자',
            'singular_name'     =>  '역자',
            'search_items'      =>  '역자 검색',
            'all_items'         =>  '전체 역자',
            'edit_item'         =>  '역자 편집',
            'update_item'       =>  '역자 수정',
            'add_new_item'      =>  '새 역자',
            'new_item_name'     =>  '새 역자 이름',
            'menu_name'         =>  '역자',
            'popular_items' => '많이 사용한 역자',
            'separate_items_with_commas' => '역자를 쉼표로 구분해 주세요',
            'add_or_remove_items' => '역자 추가 제거',
            'choose_from_most_uses' => '가장 많이 사용한 역자중에 고르기'
        ],
        'show_admin_column' => true,
    ]);

    register_taxonomy('book_subject', 'book', [
        'labels' => [
            'name'              =>  '주제',
            'singular_name'     =>  '주제',
            'search_items'      =>  '주제 검색',
            'all_items'         =>  '전체 주제',
            'parent_item'       =>  '상위 주제',
            'parent_item_colon' =>  '상위 주제:',
            'edit_item'         =>  '주제 편집',
            'update_item'       =>  '주제 수정',
            'add_new_item'      =>  '새 주제',
            'new_item_name'     =>  '새 주제 이름',
            'menu_name'         =>  '주제',
        ],
        'show_admin_column' => true,
        'hierarchical' => true
    ]);
});

 

 

 

비계층형 구조인 book_author 와 book_translator 는 

popular_items, separate_items,with_commas, add_or_remove_items 가 추가되어있고

 

계층형 구조인 booksubject는

parent_item, parent_item_colon가 추가되어있다 

 

 

반응형