WordPressカスタム投稿タイプ作成方法

カテゴリ:技術
タグ:タグなし
2025/06/01 08:00
作成者:ken

プレビュー

以下の要件に従い、WordPressでカスタム投稿タイプをfunctions.phpに記述して登録する方法を解説してください。 ・register_post_type関数の使い方 ・カスタムタクソノミーの登録 ・固定ページのテンプレート設定
0 いいね
まだいいねはありません。

レスポンス例

functions.phpに以下を追加します。
```php
function create_custom_post_type() {
  $args = array(
    'labels' => array('name' => '商品', 'singular_name' => '商品'),
    'public' => true,
    'has_archive' => true,
    'supports' => array('title', 'editor', 'thumbnail'),
  );
  register_post_type('product', $args);
}
add_action('init', 'create_custom_post_type');
```
カスタムタクソノミーは`register_taxonomy('genre', 'product', $taxonomy_args);`で実装します。固定ページテンプレートはテーマフォルダに`single-product.php`として作成し、カスタム投稿タイプの表示をカスタマイズします。