如何在WordPress图像附件编辑器中添加“链接目标”字段?

时间:2010-10-19 14:33:06

标签: wordpress-theming

我正在使用WordPress“附件”功能,允许我的主题的最终用户上传将显示在帖子内容上方的图片(不会插入帖子本身)。

我遇到的唯一问题是没有一个字段允许最终用户指定当最终用户点击其中一个附加图像时应该加载的链接。我想将此字段添加到帖子附件编辑器(列出附加到帖子的图像的“图库”)。

或者,也许另外,我希望能够通过媒体经理列表查看图像时做同样的事情。

目前,我正在使用“description”字段来存储图像的超链接。并像这样检索它(完美地工作,但描述对于链接目的地不是语义):

if ($images = get_children(array('post_parent' => get_the_ID(),'post_type' => 'attachment','post_mime_type' => 'image', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC'    )))
    {
    foreach( $images as $image ) :
        echo "<a href='".$image->post_content."'><img src='".wp_get_attachment_url($image->ID, 'medium')."' /></a>";
    endforeach;
    }   
}

1 个答案:

答案 0 :(得分:0)

function my_image_attachment_fields_to_edit($form_fields, $post) {  
    $form_fields["custom1"] = array(  
        "label" => __("Image Links To"),  
        "input" => "text", // this is default if "input" is omitted  
        "value" => get_post_meta($post->ID, "_custom1", true)  
    );    
    return $form_fields;  
}  

function my_image_attachment_fields_to_save($post, $attachment) {  
    if( isset($attachment['custom1']) ){  
        update_post_meta($post['ID'], '_custom1', $attachment['custom1']);  
    }  
    return $post;  
} 

add_filter("attachment_fields_to_edit", "my_image_attachment_fields_to_edit", null, 2); 
add_filter("attachment_fields_to_save", "my_image_attachment_fields_to_save", null, 2);