본문 바로가기

wordpress/wordpress기초

워드프레스 - 테마 만들기 - 커스텀 포스트

반응형

Custom Post 란 

워드프레스에서 기본 제공되는 포스트만으로는 한계가 잇기 때문에 직접 만들어서 사용한다 

 

 

모든 글들, 페이지, 포스트등은 전부 wp-post에 담기게 된다.

 

기본 포스트 타입

 

  • Post (누적된다)

  • Page   (페이지 - 한번 써두고 변하지 않음, 소개 같은것)

  • attachment ( 첨부파일)

  • revision (post, page의 과거 버전 기록)

  • nav_menu_item (테마 디자인(Appearance) 의 메뉴(Menu) 에서 생성한 메뉴 항목

 

메뉴 추가하기 (nav_menu_item 테스트)

테스트를 위해 잠시 기존 테마로 변경해 주었다.

 

 

사용자 정의하기를 눌러준다. 

 

 

test menu를 생성해준다.

 

 

 

 

상단 메뉴 또한 test menu를 적용시켜준다.

그러고 post_type을 조회해보면 

 

 

nav_menu_item이 추가된것을 확인 할 수 있다.

 

 

Custom Post 만들기 

다시 Bookstore 테마를 활성화 시켜준다.

 

functions.php 만들기

워드프레스가 기본으로 include 하는 php 파일

 

기본적으로 wordpress에서는 functons.php 를 사용하기 때문에 

 

register_post_type

새 포스트 타입을 등록하는 함수 

 

add_action

특정 순간에 함수를 실행할 수 있도록 시점과 함수를 주입 

 

 

wordpress에서는 기본적으로 functions.php를 include 하지 않더라도 

자동으로 읽어오기 때문에 사용이 가능하다 .

 

functions.php

 

 

functions.php

 

index.php

 

 

 

정상적으로 Hell World 가 출력이 된다. 

 

Custom Post type 등록하기 

 

register_post_type 을 함수에 선언해서 특정 시에 add_action을 통해 

커스텀 타입을 등록해 준다

또한 public을 true로 해줘야만 관리자 페이지에서 정상적으로 조회가 가능하다 

label은 제목을 변경해준다 (기본값은 글로 되어있다)

functions.php

<?php
function getHelloWorld(){
    return '<h1>Hello World</h1>';
}

function bookstore_register_post_type(){
    register_post_type('book', [
        'public' =>true,
        'label' =>'책'
    ]);
}
add_action('init', 'bookstore_register_post_type');

 

정상적으로 책이 주가된것을 확인 할 수 있다. 

 

developer.wordpress.org/reference/functions/register_post_type/

 

WordPress Developer Resources | Official WordPress Developer Resources

Official WordPress developer resources including a code reference, handbooks (for APIs, plugin and theme development, block editor), and more.

developer.wordpress.org

책 레이블 상세하게 붙이기

 

책의 레이블 내용들을 아래와 같이 상세하게 변경 시 킬 수 있다. 

functions.php

<?php
function getHelloWorld(){
    return '<h1>Hello World</h1>';
}

function bookstore_register_post_type(){
    register_post_type('book', [
        '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,
    ]);
}
add_action('init', 'bookstore_register_post_type');

 

 

메뉴 위치 변경하기

menu_position

<?php
function getHelloWorld(){
    return '<h1>Hello World</h1>';
}

function bookstore_register_post_type(){
    register_post_type('book', [
        '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,
    ]);
}
add_action('init', 'bookstore_register_post_type');

 

해당 캡쳐를 기준으로 메뉴의 위치를 얼추 지정할 수 있다.

 

 

메뉴 아이콘 변경하기 

 

 

developer.wordpress.org/resource/dashicons/#plus

 

WordPress Developer Resources | Official WordPress Developer Resources

Official WordPress developer resources including a code reference, handbooks (for APIs, plugin and theme development, block editor), and more.

developer.wordpress.org

'menu_icon'

옵션에 해당 링크에 적혀있는 예약되어있는 아이콘을 넣어주면 아이콘을 변경할수 있다. 

 

 

1. 미리 지정되어있는 아이콘으로 변경하기 

책 모양으로 변경을 해보겠다.

 

functions.php

<?php
function getHelloWorld(){
    return '<h1>Hello World</h1>';
}

function bookstore_register_post_type(){
    register_post_type('book', [
        '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'
    ]);
}
add_action('init', 'bookstore_register_post_type');

 

 

 

아이콘이 변경되었다.

 

2. 아이콘 경로를 통해 가져오기 

 

flatIcon을 통해 이미지를 다운해줬다.

 

www.flaticon.com/free-icon/open-book_171322?term=book&page=1&position=2

 

Open Book free vector icons designed by Freepik

Download this free icon in SVG, PSD, PNG, EPS format or as webfonts. Flaticon, the largest database of free vector icons.

www.flaticon.com

<?php
function getHelloWorld(){
    return '<h1>Hello World</h1>';
}

function bookstore_register_post_type(){
    register_post_type('book', [
        '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' => get_template_directory_uri().'/images/open-book.svg'
    ]);
}
add_action('init', 'bookstore_register_post_type');

get_template_directory_uri 를 통해 해당 경로에있는 images폴더의 svg를 가져와서 아이콘 등록을 해주었다.

 

책 부분이 정상적으로 변경된 것을 확인 할 수 있다.

반응형