アタッチメントページへのカスタムフィールドの追加と表示例

アタッチメントページでのカスタムフィールドサポートを追加

テーマのfunctions.phpへ

add_action('init', 'my_custom_init');
function my_custom_init() {
	add_post_type_support( 'attachment', 'custom-fields' );
}

表示されないときは、表示オプションタブをクリックして、チェックが入っているか確認してください

固定ページなどでの表示例

カスタムフィールドキーhelloを表示します

<?php

$args= array( 'post_type'=> 'attachment' );
	
$my_attachments= get_posts( $args ) ;

$html= '<dl><dt>%1$s</dt><dd>%2$s</dd><dd>%3$s</dd></dl>';

foreach ( $my_attachments as $post ) {
	setup_postdata($post);
	$id= get_the_ID();
	
	printf( $html,
			wp_get_attachment_image( $id ),
			get_the_date(),
			get_post_meta( $id, 'hello', true)
	);
}
wp_reset_postdata();			
?>

Post Types

[emulsion_relate_posts]