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

发布于 2024-09-28 07:23:29 字数 699 浏览 0 评论 0原文

我正在使用 WordPress“附件”功能来允许我的主题的最终用户上传将出现在帖子内容上方的图像(而不是插入到帖子本身中)。

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

或者,也许此外,我希望能够在通过媒体管理器列表查看图像时执行相同的操作。

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

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;
    }   
}

I'm using the WordPress "attachment" feature to allow end users of my theme to upload images that will appear above the post content (not inserted into the post itself).

The only problem I have is that there is not a field to allow the end user to specify the link that should be loaded when the end user clicks on one of the attached images. I'd like to add this field to the post attachment editor (the one that lists the "Gallery" of images attached to the post).

Alternately, and perhaps in addition, I'd like to be able to do the same thing when viewing images via the Media manager listing.

Currently, I'm using the "description" field to store the hyperlink to the image. and retrieving it like so (works perfectly but description is not semantic to link destination):

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;
    }   
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叹沉浮 2024-10-05 07:23:29
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); 
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); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文