将图像URL自动发布到自定义字段

时间:2012-01-30 23:01:39

标签: wordpress

在Wordpress中..我正在寻找一种方法将帖子中的第一个上传图像自动添加到名为“Image”的自定义字段中。

有谁知道这个?

1 个答案:

答案 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);
}
}