【发布时间】:2016-03-01 11:51:32
【问题描述】:
我在 Wordpress 中为图像创建了一个自定义字段,该字段是使用以下代码正确创建和保存的。
但是,如果该图像上的字段存在值,我需要将此字段 vimeo-id 插入到插入 WYSIWYG 编辑器的任何 <img> 标记中(作为 data 属性)。另外,如果该值存在,我还想将 .video-thumb 类添加到图像中。
电流输出类似于以下内容:
<img src="ImageURL.jpg" alt="Image Alt" width="1920" height="1080" class="size-full wp-image-0000">
所需输出(如果存在 vimeo-id):
<img src="ImageURL.jpg" alt="Image Alt" width="1920" height="1080" class="size-full wp-image-0000 video-thumb" data-vimeo-id="12345678">
有人可以帮忙吗?
自定义字段函数
/* Add custom field to attachment */
function image_attachment_add_custom_fields($form_fields, $post) {
$form_fields["vimeo-id"] = array(
"label" => __("Vimeo ID"),
"input" => "text",
"value" => get_post_meta($post->ID, "vimeo-id", true),
);
return $form_fields;
}
add_filter("attachment_fields_to_edit", "image_attachment_add_custom_fields", null, 2);
/* Save custom field value */
function image_attachment_save_custom_fields($post, $attachment) {
if(isset($attachment['vimeo-id'])) {
update_post_meta($post['ID'], 'vimeo-id', $attachment['vimeo-id']);
} else {
delete_post_meta($post['ID'], 'vimeo-id');
}
return $post;
}
add_filter("attachment_fields_to_save", "image_attachment_save_custom_fields", null , 2);
?>
【问题讨论】: