添加图像的自定义附件显示设置


11

我已经做了很多研究,但还没有解决。您可以在Attachment Display SettingsInsert Media帖子编辑器中对话框的一部分)中添加自定义选项吗?

我需要的是能够在帖子中的所有图像周围添加带有类的锚点的功能。


advancedcustomfields.com可以这样做,当您为额外的字段创建新的字段组时,选择附件位置,它也会在“插入媒体”对话框和附件编辑页面上显示额外的字段
passatgt 2014年

Answers:


1

这将在附件编辑屏幕中添加一个字段,用于将类应用于img标签。

function IMGattachment_fields($form_fields, $post) {
    $form_fields["imageClass"]["label"] = __("Image Class");
    $form_fields["imageClass"]["value"] = get_post_meta($post->ID, "_imageClass", true);
    return $form_fields;
}
add_filter("attachment_fields_to_edit", "IMGattachment_fields", null, 2);
function my_image_attachment_fields_save($post, $attachment) {
    if ( isset($attachment['imageClass']) )
    update_post_meta($post['ID'], '_imageClass', $attachment['imageClass']);
    return $post;
}
add_filter("attachment_fields_to_save", "my_image_attachment_fields_save", null, 2);

0

您只需要将此添加到主题functions.php文件中:

/**
* Attach a class to linked images' parent anchors
* e.g. a img => a.img img
*/
function give_linked_images_class($html, $id, $caption, $title, $align, $url, $size, $alt = '' ) {
    $classes = 'img'; // separated by spaces, e.g. 'img image-link'

    // check if there are already classes assigned to the anchor
    if ( preg_match('/<a.*? class=".*?">/', $html) ) {
    $html = preg_replace('/(<a.*? class=".*?)(".*?>)/', '$1 ' . $classes . '$2', $html);
    } else {
     $html = preg_replace('/(<a.*?)>/', '$1 class="' . $classes . '" >', $html);
    }
    return $html;
}

add_filter('image_send_to_editor','give_linked_images_class',10,8);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.