我有以下示例代码,它创建了一个自定义字段并添加到附件元中:
function be_attachment_field_credit( $form_fields, $post ) {
$form_fields[\'be-photographer-name\'] = array(
\'label\' => \'Photographer Name\',
\'input\' => \'text\',
\'value\' => get_post_meta( $post->ID, \'be_photographer_name\', true ),
\'helps\' => \'If provided, photo credit will be displayed\',
);
$form_fields[\'be-photographer-url\'] = array(
\'label\' => \'Photographer URL\',
\'input\' => \'text\',
\'value\' => get_post_meta( $post->ID, \'be_photographer_url\', true ),
\'helps\' => \'Add Photographer URL\',
);
return $form_fields;
}
add_filter( \'attachment_fields_to_edit\', \'be_attachment_field_credit\', 10, 2 );
function be_attachment_field_credit_save( $post, $attachment ) {
if( isset( $attachment[\'be-photographer-name\'] ) )
update_post_meta( $post[\'ID\'], \'be_photographer_name\', $attachment[\'be-photographer-name\'] );
if( isset( $attachment[\'be-photographer-url\'] ) )
update_post_meta( $post[\'ID\'], \'be_photographer_url\', esc_url( $attachment[\'be-photographer-url\'] ) );
return $post;
}
add_filter( \'attachment_fields_to_save\', \'be_attachment_field_credit_save\', 10, 2 );
我试过打电话
var_dump(get_post_meta($post->ID));
, 但我假设meta在其他地方,因为没有任何输入。
According to the codex, 我可以打电话:
wp_get_attachment_metadata( $attachment_id, $unfiltered );
.
然而,我不知道如何获得$attachment_id
.
我的最终目标是将此元数据添加到高级自定义字段库插件中,该插件将附件元数据添加到post元数据中(我相信)-但这是一条很长的路。