acf_radio_input()


ACF(Advanced Custom Fields)のacf_radio_input()は、ラジオボタン形式で選択フィールドを作成するための関数です。この関数は特定の条件やカスタム設定で選択肢を表示するのに役立ちます。

構文

acf_radio_input( array $args );

引数の説明:

  • $args (array) — ラジオボタンフィールドの設定を指定する配列。
  • $args['choices'] (array) — ラジオボタンの選択肢を指定する配列。
  • $args['value'] (mixed) — 初期選択値。
  • $args['name'] (string) — フィールド名。
  • $args['id'] (string) — HTML要素のID。
  • $args['class'] (string) — CSSクラス。

例1: シンプルなラジオボタンの作成

ラジオボタンフィールドを生成します。

acf_radio_input( array(
    'choices' => array(
        'option1' => '選択肢1',
        'option2' => '選択肢2'
    ),
    'name' => 'custom_field'
) );

例2: 初期値の設定

初期選択値を設定したラジオボタンを作成します。

acf_radio_input( array(
    'choices' => array(
        'yes' => 'はい',
        'no' => 'いいえ'
    ),
    'value' => 'yes',
    'name' => 'user_preference'
) );

例3: IDとクラスを指定する

特定のHTML属性を持つラジオボタンを作成します。

acf_radio_input( array(
    'choices' => array(
        'red' => '赤',
        'blue' => '青'
    ),
    'name' => 'color_selection',
    'id' => 'color-radio',
    'class' => 'color-radio-class'
) );

例4: 条件付きの選択肢を設定

条件に基づいて選択肢を生成します。

$choices = is_user_logged_in() ? array('logged_in' => 'ログイン済み') : array('guest' => 'ゲスト');
acf_radio_input( array(
    'choices' => $choices,
    'name' => 'user_status'
) );

例5: 投稿のカスタムフィールドで使用

投稿のカスタムフィールド値を使用してラジオボタンを生成します。

$custom_choices = get_post_meta( get_the_ID(), 'custom_choices', true );
acf_radio_input( array(
    'choices' => $custom_choices,
    'name' => 'dynamic_field'
) );

注意事項

  • 配列内の選択肢キーと値を正確に設定してください。
  • 同じnameを持つフィールドが複数存在すると、正しく動作しない可能性があります。
  • $argsが空の場合、エラーが発生する可能性があります。