ワードプレスのadd_post_type_support関数の使用方法・解説

概要

add_post_type_support() 関数は、特定のカスタム投稿タイプに追加の機能や特徴を付与するために使用されます。この関数によって、投稿タイプに特徴を追加することができ、たとえば、エディタ機能やカスタムフィールド、アイキャッチ画像などのサポートを簡単に追加できます。よく使われる機能には以下のようなものがあります。

  1. サムネイル(アイキャッチ画像)
  2. カスタムフィールド
  3. エディタ
  4. コメント
  5. 埋め込みメディア
  6. フォーマット
  7. 項目メタ
  8. カスタムタクソノミー

構文

add_post_type_support( string $post_type, mixed $supports );

パラメータ

  • $post_type: (string)投稿タイプのスラグ(例: ‘book’)。
  • $supports: (mixed)追加する機能のスラグの配列または単一のスラグ(例: ‘thumbnail’、’editor’)。

戻り値

  • (bool)関数が成功した場合はtrue、失敗した場合はfalseを返します。

関連する関数

ワードプレスのバージョン

  • この関数は、WordPress 2.9 以降で使用可能です。

コアファイルのパス

  • /wp-includes/post.php

この関数のアクションでの使用可能性

アクション 使用例
mu_plugin_loaded
registered_post_type
plugins_loaded
wp_roles_init
setup_theme
after_setup_theme
set_current_user
init
register_sidebar
wp_loaded
send_headers
parse_query
pre_get_posts
wp
template_redirect
get_header
wp_head

サンプルコード

サンプルコード 1: アイキャッチ画像のサポート追加

function my_custom_post_type() {
    register_post_type('book', array(
        'labels' => array(
            'name' => __('Books'),
            'singular_name' => __('Book'),
        ),
        'public' => true,
        'has_archive' => true,
    ));
    add_post_type_support('book', 'thumbnail');
}
add_action('init', 'my_custom_post_type');

このコードは、カスタム投稿タイプ「book」を登録し、アイキャッチ画像のサポートを追加しています。

サンプルコード 2: カスタムフィールドのサポート追加

function my_custom_post_type_with_meta() {
    register_post_type('movie', array(
        'labels' => array(
            'name' => __('Movies'),
            'singular_name' => __('Movie'),
        ),
        'public' => true,
    ));
    add_post_type_support('movie', 'custom-fields');
}
add_action('init', 'my_custom_post_type_with_meta');

このコードは、カスタム投稿タイプ「movie」を登録し、カスタムフィールドのサポートを追加します。

サンプルコード 3: コメントのサポート追加

function my_custom_post_type_with_comments() {
    register_post_type('review', array(
        'labels' => array(
            'name' => __('Reviews'),
            'singular_name' => __('Review'),
        ),
        'public' => true,
    ));
    add_post_type_support('review', 'comments');
}
add_action('init', 'my_custom_post_type_with_comments');

このコードにより、「review」投稿タイプにコメント機能が追加されます。

サンプルコード 4: エディタのサポート追加

function my_custom_post_type_with_editor() {
    register_post_type('article', array(
        'labels' => array(
            'name' => __('Articles'),
            'singular_name' => __('Article'),
        ),
        'public' => true,
    ));
    add_post_type_support('article', 'editor');
}
add_action('init', 'my_custom_post_type_with_editor');

このコードは、「article」投稿タイプにエディタ機能を追加します。

サンプルコード 5: 複数のサポートの追加

function my_custom_post_type_with_multiple_support() {
    register_post_type('project', array(
        'labels' => array(
            'name' => __('Projects'),
            'singular_name' => __('Project'),
        ),
        'public' => true,
    ));
    add_post_type_support('project', array('thumbnail', 'editor', 'comments'));
}
add_action('init', 'my_custom_post_type_with_multiple_support');

このコードは、「project」投稿タイプにアイキャッチ画像、エディタ、コメントのサポートを追加します。

この関数について質問する


上の計算式の答えを入力してください