要向附件添加自定义字段,请在函数中放置以下代码。php。
add_filter(\'attachment_fields_to_edit\', \'edit_media_custom_field\', 11, 2 );
add_filter(\'attachment_fields_to_save\', \'save_media_custom_field\', 11, 2 );
function edit_media_custom_field( $form_fields, $post ) {
$form_fields[\'custom_field\'] = array( \'label\' => \'Custom Field\', \'input\' => \'text\', \'value\' => get_post_meta( $post->ID, \'_custom_field\', true ) );
return $form_fields;
}
function save_media_custom_field( $post, $attachment ) {
update_post_meta( $post[\'ID\'], \'_custom_field\', $attachment[\'custom_field\'] );
return $post;
}
要输出该数据,可以在循环中使用以下内容:
get_post_meta( get_the_ID(), \'_custom_field\', true ) );