在Wordpress中..我正在寻找一种方法将帖子中的第一个上传图像自动添加到名为“Image”的自定义字段中。
有谁知道这个?
答案 0 :(得分:0)
我找到了自己的答案
以下是将缩略图网址添加到名为Image。
的自定义字段的最终代码function w_thumbnail_src() {
if (has_post_thumbnail()) {
$thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'emphasis');
return $thumb[0]; // thumbnail url
} else {
return ''; // or a default thumbnail url
}
}
add_action('publish_page', 'add_custom_field_automatically', 'w_thumbnail_src');
add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_id) {
global $wpdb;
if(!wp_is_post_revision($post_id)) {
add_post_meta($post_id, 'Image', w_thumbnail_src(), true);
}
}