WordPressにカスタム投稿を作る

WordPressにカスタム投稿を作成する方法は

  • プラグインを使う
  • コードを使う

の2通りあります。

この記事ではカスタム投稿をコードを使って追加する方法を紹介します。

プラグインでカスタム投稿を追加する場合は「Custom Post Type UI」プラグインをインストールする必要があります。

目次

サンプルコード

カスタム投稿タイプで商品用の投稿を作るサンプルです。

<?php
/*-------------------------------------------*/
/* カスタム投稿タイプ「商品情報」を追加
/*-------------------------------------------*/
function create_post_type_product()
{
  $productSupports = [
    'title',
    'editor',
    'thumbnail',
    'revisions',
    'custom-fields'
  ];

   // 投稿タイプを登録
  register_post_type( 'product',
    array(
      'label' => '商品一覧',
      'public' => true,
      'has_archive' => true,
      'menu_position' => 5,
      'supports' => $productSupports,
      'rewrite' => array('slug' => 'product','with_front' => false),
    )
  );

  // カスタム分類「商品情報カテゴリー」を追加
  register_taxonomy(
    'product_category',
    'product',
    array(
      'label' => 'カテゴリ',
      'labels' => array(
        'all_items' => 'カテゴリ一覧',
        'add_new_item' => '新規カテゴリを追加'
      ),
      'hierarchical' => true
    )
  );
  // カスタム分類「商品情報タグ」を追加
  register_taxonomy(
    'product_tag',
    'product',
    array(
      'hierarchical' => false,
      'update_count_callback' => '_update_post_term_count',
      'label' => '商品のタグ',
      'singular_label' => '商品のタグ',
      'public' => true,
      'show_ui' => true
    )
  );
}

add_action( 'init', 'create_post_type_product' );


/*-------------------------------------------*/
/* カスタム投稿一覧にタームを表示
/*-------------------------------------------*/
function manage_product_posts_columns($columns)
{
        $columns['product_category'] = "カテゴリ";
        $columns['product_tag'] = "タグ";
        return $columns;
}
add_filter('manage_edit-product_columns', 'manage_product_posts_columns');


/*-------------------------------------------*/
/* 該当カテゴリーがない場合「なし」を表示
/*-------------------------------------------*/
function add_column_in_product($column_name, $post_id)
{
    //カテゴリー名取得
    if( 'product_category' == $column_name )
    {
        $fcategory = get_the_term_list($post_id, 'product_category', '',', ');
    }
    if( 'product_tag' == $column_name )
    {
        $fcategory = get_the_term_list($post_id, 'product_tag', '',', ');
    }

    if ( isset($fcategory) && $fcategory )
    {
        echo $fcategory;
    } else {
        echo __('None');
    }
}
add_action('manage_product_posts_custom_column',  'add_column_in_product', 10, 2);

カスタム投稿タイプを追加

サンプルコード内のcreate_post_type_product関数について解説します。

create_post_type_product関数では

  • 投稿タイプの登録
  • カスタム分類の追加

をしています。

投稿タイプの登録

  $productSupports = [
    'title',
    'editor',
    'thumbnail',
    'revisions',
    'custom-fields'
  ];

   // 投稿タイプを登録
  register_post_type( 'product',
    array(
      'label' => '商品一覧',
      'public' => true,
      'has_archive' => true,
      'menu_position' => 5,
      'supports' => $productSupports,
      'rewrite' => array('slug' => 'product','with_front' => false),
    )
  );

投稿タイプの登録ではregister_post_type関数を使って投稿タイプの登録をします。

https://wpdocs.osdn.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/register_post_type

register_post_type関数の第2引数の配列のsupportsではカスタム投稿タイプで使用できる機能を指定しています。

supportsではこれらを指定することができます。

‘title’ (タイトル)

‘editor’ (内容の編集)

‘author’ (作成者)

‘thumbnail’ (アイキャッチ画像。現在のテーマが post-thumbnails をサポートしていること)

‘excerpt’ (抜粋)

‘trackbacks’ (トラックバック送信)

‘custom-fields’ (カスタムフィールド)

‘comments’ (コメントの他、編集画面にコメント数のバルーンを表示する)

‘revisions’ (リビジョンを保存する)

‘page-attributes’ (メニューの順序。「親〜」オプションを表示するために hierarchical が true であること)

‘post-formats’ (投稿のフォーマットを追加。投稿フォーマットを参照)

カスタム分類の追加

 // カスタム分類「商品情報カテゴリー」を追加
  register_taxonomy(
    'product_category',
    'product',
    array(
      'label' => 'カテゴリ',
      'labels' => array(
        'all_items' => 'カテゴリ一覧',
        'add_new_item' => '新規カテゴリを追加'
      ),
      'hierarchical' => true
    )
  );
  // カスタム分類「商品情報タグ」を追加
  register_taxonomy(
    'product_tag',
    'product',
    array(
      'hierarchical' => false,
      'update_count_callback' => '_update_post_term_count',
      'label' => '商品のタグ',
      'singular_label' => '商品のタグ',
      'public' => true,
      'show_ui' => true
    )
  );

カスタム分類の追加ではregister_taxonomy関数を使います。

https://wpdocs.osdn.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/register_taxonomy

カテゴリもタグも同じregister_taxonomy関数で追加をします。hierarchicaltrueだとカテゴリー、hierarchicalfalseだとタグで登録をします。

hierarchical
真偽値) (オプション)true ならカテゴリーのような階層あり(子を持つ)タクソノミー、false ならタグのような階層化しないタクソノミー。初期値: false

register_taxonomy関数の第一引数の名前は次の「カスタム投稿一覧にタームを表示」で使用します。

カスタム投稿一覧にタームを表示

/*-------------------------------------------*/
/* カスタム投稿一覧にタームを表示
/*-------------------------------------------*/
function manage_product_posts_columns($columns)
{
        $columns['product_category'] = "カテゴリ";
        $columns['product_tag'] = "タグ";
        return $columns;
}
add_filter('manage_edit-product_columns', 'manage_product_posts_columns');

manage_product_posts_columns関数では投稿一覧で表示させる内容を追加しています。

サンプルコードではカスタム投稿一覧でカテゴリとタグを表示するように設定しています。

$columnsのキーはregister_taxonomy関数の第一引数で指定した名前を使って値を入れてください。

最後のadd_action関数の第一引数は命名規則があり、

do_action( “manage_edit-$post->post_type}_columns”, string $column_name, int $post_id )

となるように命名しましょう。

該当カテゴリーがない場合「なし」を表示

/*-------------------------------------------*/
/* 該当カテゴリーがない場合「なし」を表示
/*-------------------------------------------*/
function add_column_in_product($column_name, $post_id)
{
    //カテゴリー名取得
    if( 'product_category' == $column_name )
    {
        $fcategory = get_the_term_list($post_id, 'product_category', '',', ');
    }
    if( 'product_tag' == $column_name )
    {
        $fcategory = get_the_term_list($post_id, 'product_tag', '',', ');
    }

    if ( isset($fcategory) && $fcategory )
    {
        echo $fcategory;
    } else {
        echo __('None');
    }
}
add_action('manage_product_posts_custom_column',  'add_column_in_product', 10, 2);

add_column_in_product関数ではカテゴリやタグを検索した時に該当しない場合は「なし」を返すようにしています。

最後のadd_action関数の第一引数は命名規則があり、

do_action( “manage_{$post->post_type}_posts_custom_column”, string $column_name, int $post_id )

となるように命名しましょう。

https://developer.wordpress.org/reference/hooks/manage_post-post_type_posts_custom_column/

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

コメント

コメントする


The reCAPTCHA verification period has expired. Please reload the page.

目次