acf_add_local_field_group()


WordPressの関数acf_add_local_field_group()は、Advanced Custom Fields (ACF) プラグインを使用してカスタムフィールドグループを定義するために使用されます。この関数は、フィールドグループをプログラム的に追加するために利用されます。

構文

acf_add_local_field_group( array $args );
  • $args (array) — フィールドグループの設定を含む配列。

例1: シンプルなカスタムフィールドグループの追加

フィールドグループを定義し、投稿タイプに追加する例です。

<?php
acf_add_local_field_group( array(
    'key' => 'group_1',
    'title' => 'Sample Field Group',
    'fields' => array(
        array(
            'key' => 'field_1',
            'label' => 'Sample Field',
            'name' => 'sample_field',
            'type' => 'text',
        ),
    ),
    'location' => array(
        array(
            array(
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'post',
            ),
        ),
    ),
) );
?>

例2: 複数のフィールドを持つフィールドグループ

複数のフィールドを持つフィールドグループを追加する例です。

<?php
acf_add_local_field_group( array(
    'key' => 'group_2',
    'title' => 'Multiple Fields Group',
    'fields' => array(
        array(
            'key' => 'field_2',
            'label' => 'Text Field',
            'name' => 'text_field',
            'type' => 'text',
        ),
        array(
            'key' => 'field_3',
            'label' => 'Textarea Field',
            'name' => 'textarea_field',
            'type' => 'textarea',
        ),
    ),
    'location' => array(
        array(
            array(
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'page',
            ),
        ),
    ),
) );
?>

例3: フィールドグループに条件を追加

特定の条件に基づいてフィールドグループを表示する例です。

<?php
acf_add_local_field_group( array(
    'key' => 'group_3',
    'title' => 'Conditional Fields Group',
    'fields' => array(
        array(
            'key' => 'field_4',
            'label' => 'Conditionally Visible Field',
            'name' => 'conditional_field',
            'type' => 'text',
            'conditional_logic' => array(
                array(
                    array(
                        'field' => 'field_5',
                        'operator' => '==',
                        'value' => 'show',
                    ),
                ),
            ),
        ),
        array(
            'key' => 'field_5',
            'label' => 'Show Condition',
            'name' => 'show_condition',
            'type' => 'select',
            'choices' => array(
                'show' => 'Show',
                'hide' => 'Hide',
            ),
        ),
    ),
    'location' => array(
        array(
            array(
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'post',
            ),
        ),
    ),
) );
?>

例4: 複数の場所でフィールドグループを表示

フィールドグループを複数の場所で表示する例です。

<?php
acf_add_local_field_group( array(
    'key' => 'group_4',
    'title' => 'Multiple Location Group',
    'fields' => array(
        array(
            'key' => 'field_6',
            'label' => 'Multiple Locations Field',
            'name' => 'multiple_locations_field',
            'type' => 'text',
        ),
    ),
    'location' => array(
        array(
            array(
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'post',
            ),
        ),
        array(
            array(
                'param' => 'taxonomy',
                'operator' => '==',
                'value' => 'category',
            ),
        ),
    ),
) );
?>

例5: フィールドタイプの変更

フィールドタイプを変更する例です。

<?php
acf_add_local_field_group( array(
    'key' => 'group_5',
    'title' => 'Field Type Change Group',
    'fields' => array(
        array(
            'key' => 'field_7',
            'label' => 'Changed Field Type',
            'name' => 'changed_field_type',
            'type' => 'number',
        ),
    ),
    'location' => array(
        array(
            array(
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'page',
            ),
        ),
    ),
) );
?>

予防策

  • 必ずフィールドグループのキーが一意であることを確認してください。
  • ACFがインストールされていない場合、エラーが発生する可能性があります。