Web制作忘備録

カスタム投稿タイプの追加【プラグイン無】

トップ MEMORANDAM WORDPRESS

カスタム投稿タイプの追加【プラグイン無】

プラグインなしで追加

function.php内に書くことで実装される。
各メニューのアイコンは下記から確認
https://developer.wordpress.org/resource/dashicons/#twitch

function.php

function create_post_type() {

    $exampleSupports = [
        'title',
        'editor',
        'thumbnail',
        'revisions',
        'page-attributes'
    ];

    /*---------- カスタム投稿タイプ追加 ----------*/
    register_post_type( 'works', // URLになる部分
        array(
            'label' => 'WORKS', // 管理画面の左メニューに表示されるテキスト
            'labels' => array(
                'all_items' => '制作実績一覧'// 管理画面の左メニューの下層に表示されるテキスト
            ),
            'public' => true,
            'has_archive' => true,
            'hierarchical' => true,
            'menu_icon' => 'dashicons-text-page',
            'show_in_rest' => true,
            'supports' => $exampleSupports,
        )
    );
    register_taxonomy(
        'works_category',
        'works',
        array(
            'label' => 'カテゴリ',
            'labels' => array(
                'all_items' => 'カテゴリ一覧',
                'add_new_item' => '新規カテゴリを追加'
            ),
            'hierarchical' => true,
            'show_in_rest' => true,
            'rewrite' => array('with_front' => false,'slug' => 'works_category')
        )
    );
}
add_action( 'init', 'create_post_type' );