Web production note

 【更新日 :

【WordPress】ACF ターム編集ページに設定した項目の値を取得する

WordPressのプラグインAdvanced Custom Fields (ACF) でターム編集ページに設定した項目の値を取得するサンプルコードです。

記事が属しているカスタムタクソノミーのタームの情報を出力する

get_field の第二引数の箇所にターム名とタームIDを設定します。
※ターム名とタームIDはアンダーバ「_」でつなぎます。

<?php
	//ターム編集ページの値を出力する
	$terms = get_the_terms( $post -> ID, 'タクソノミー名' );
	foreach( $terms as $term ) {
		$term_id = esc_html( $term -> term_taxonomy_id );
	}
?>

<p><?php the_field('フィールド名', 'ターム名_'.$term_id); ?></p>

ループの中で取得する(ターム一覧の出力中に表示)

【WordPress】カスタムタクソノミー (カスタム分類) のターム一覧を表示させる
上記ページのコードを交えたサンプルです。

<?php
	//カスタムタクソノミー (カスタム分類) のターム一覧を表示
	$taxonomies = array(
	'タクソノミー名'
	);
	$args = array(
	'get' => 'all'
	);
	$terms = get_terms($taxonomies,$args);
	
	foreach($terms as $key => $value):
?>
	<?php the_field('フィールド名', $value); ?>
<?php endforeach; ?>

参考リンク