acf_get_user_result()


関数acf_get_user_result()は、Advanced Custom Fields(ACF)プラグインを使用して、特定のユーザーに関連付けられたカスタムフィールドの値を取得するために使用されます。

構文

acf_get_user_result( int $user_id, string $field_name );

引数の説明:

  • $user_id (int) — ユーザーIDを指定します。
  • $field_name (string) — 取得したいカスタムフィールドの名前を指定します。

例1: 特定のユーザーのカスタムフィールドを取得

この例では、ユーザーIDが1のユーザーの「bio」フィールドの値を取得します。

<?php $bio = acf_get_user_result( 1, 'bio' ); echo $bio; ?>

例2: 現在のユーザーのカスタムフィールドを取得

現在ログインしているユーザーの「bio」フィールドの値を取得します。

<?php $current_user_id = get_current_user_id(); $bio = acf_get_user_result( $current_user_id, 'bio' ); echo $bio; ?>

例3: ユーザーのカスタムフィールドが存在するか確認

ユーザーIDが2のユーザーに「bio」フィールドが存在するか確認します。

<?php $bio = acf_get_user_result( 2, 'bio' ); if ( $bio ) { echo '存在します'; } else { echo '存在しません'; } ?>

例4: カスタムフィールドの値を配列として取得

ユーザーIDが3のユーザーの「skills」フィールドの値を配列として取得します。

<?php $skills = acf_get_user_result( 3, 'skills' ); print_r( $skills ); ?>

例5: カスタムフィールドの値を条件付きで表示

ユーザーIDが4のユーザーの「bio」フィールドが空でない場合にのみ表示します。

<?php $bio = acf_get_user_result( 4, 'bio' ); if ( !empty( $bio ) ) { echo $bio; } ?>

注意点:

  • ユーザーIDが存在しない場合、またはフィールドが存在しない場合、関数はfalseを返します。