acf_get_choices_from_grouped_terms()


関数acf_get_choices_from_grouped_terms()は、WordPressのAdvanced Custom Fields(ACF)プラグインで使用される関数で、グループ化されたタームから選択肢を取得するために使用されます。

構文

acf_get_choices_from_grouped_terms( array $args );

引数の説明:

  • $args(array) — タームを取得するための引数を指定します。以下のキーを持つ連想配列です。
  • taxonomy(string|array) — タームを取得するタクソノミーを指定します。
  • orderby(string) — タームの並び順を指定します。デフォルトは’name’です。
  • order(string) — 並び順の方向を指定します。’ASC’または’DESC’が使用可能です。
  • hide_empty(bool) — 空のタームを非表示にするかどうかを指定します。
  • parent(int) — 特定の親タームの子タームのみを取得します。

例1: 基本的な使用法

カテゴリータクソノミーからタームを取得し、選択肢として表示します。

$choices = acf_get_choices_from_grouped_terms( array( 'taxonomy' => 'category' ) ); print_r( $choices );

例2: タームを親タームでグループ化

親タームに基づいてタームをグループ化し、選択肢を取得します。

$choices = acf_get_choices_from_grouped_terms( array( 'taxonomy' => 'category', 'parent' => 0 ) ); print_r( $choices );

例3: 空のタームを非表示にする

空のタームを非表示にして選択肢を取得します。

$choices = acf_get_choices_from_grouped_terms( array( 'taxonomy' => 'category', 'hide_empty' => true ) ); print_r( $choices );

例4: タームを名前でソート

タームを名前で昇順にソートして選択肢を取得します。

$choices = acf_get_choices_from_grouped_terms( array( 'taxonomy' => 'category', 'orderby' => 'name', 'order' => 'ASC' ) ); print_r( $choices );

例5: 複数のタクソノミーからタームを取得

複数のタクソノミーからタームを取得し、選択肢として表示します。

$choices = acf_get_choices_from_grouped_terms( array( 'taxonomy' => array( 'category', 'post_tag' ) ) ); print_r( $choices );

注意点

  • タームが存在しない場合、空の配列が返されます。
  • 引数を正しく指定しないと、期待した結果が得られない場合があります。