STRIX | ストリックスマーケティング HP制作会社

STRIX | ストリックスマーケティング HP制作会社

wordpress関連

カスタム投稿タイプの追加の仕方

カスタム投稿タイプを追加するには以下のコードをfunctions.phpに追記する。


add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'sample', //カスタム投稿タイプ名を指定
        array(
                'labels' = array(
                'name' = __( 'sample関連' ),
            'singular_name' = __( 'sample関連' )
        ),
        'public' = true,
        'has_archive' = true, /* アーカイブページを持つ */
        'menu_position' = 5, //管理画面のメニュー順位
        'supports' = array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields' ,'comments' ),
        )
    );
/* カテゴリタクソノミー(カテゴリー分け)を使えるように設定する */
  register_taxonomy(
    'sample_cat', /* タクソノミーの名前 */
    'sample', /* 使用するカスタム投稿タイプ名 */
    array(
      'hierarchical' = true, /* trueだと親子関係が使用可能。falseで使用不可 */
      'update_count_callback' = '_update_post_term_count',
      'label' = 'wordpressカテゴリー',
      'singular_label' = 'wordpressカテゴリー',
      'public' = true,
      'show_ui' = true
    )
  );
/* カスタムタクソノミー、タグを使えるようにする */
  register_taxonomy(
    'sample_tag', /* タクソノミーの名前 */
    'sample', /* 使用するカスタム投稿タイプ名 */
    array(
      'hierarchical' = false,
      'update_count_callback' = '_update_post_term_count',
      'label' = 'wordpressタグ',
      'singular_label' = 'wordpressタグ',
      'public' = true,
      'show_ui' = true
    )
  );