我在用 register_taxonomy_for_object_type() 将类别分类字段添加到媒体上传(附件)。我正在使用此代码这样做:

add_action('init', 'reg_tax');
function reg_tax() {
   register_taxonomy_for_object_type('category', 'attachment');
}

在查看图像时,这起作用并添加了一个简单的文本字段到媒体页面中。我真正想要的是使其显示实际类别Metabox,以便我可以选择要使用的类别,而不仅仅是将它们键入平原。我还发现,将一个类别的slug放入此文本字段,例如 my-category-name 最终显示为实际类别名称 My Category Name 保存后,这使简单的文本字段更少成为有用的选择。

我一直在看 add_post_type_support() 添加Metaboxes的功能,并且已经看到它用于自定义邮政类型,我只是看不到是否可以为附件添加相同的功能。

有帮助吗?

解决方案

编辑:2017年12月9日,请参阅此答案,以获取更新的解决方案: 如何在新媒体库的附件上使用分类法?

我将在这里回答自己的问题,因为我设法弄清楚了我一直在尝试做什么的解决方案。我得出的结论是,不可能获得启用附件的类别Metabox。但是,我发现通过使用的基本字段非常容易 register_taxonomy_for_object_typeadd_post_type_support:

add_action('admin_init', 'reg_tax');
function reg_tax() {
   register_taxonomy_for_object_type('category', 'attachment');
   add_post_type_support('attachment', 'category');
}

添加的字段显示了这样的:

alt text

这只是一个纯文本字段,但我发现您可以在其中键入现有类别的名称,然后在更新附件时成功保存它(唯一奇怪的行为是它将其呈现为正常版本,而不是保存后的sl。

一旦我意识到可以这样保存类别,我就认为我可以将所有可用类别的列表作为复选框,并检查已选择的类别。然后,我使用了一些jQuery来获取检查类别的值,并将所有类别的slugs放入类别字段中。为了使这看起来更加无缝,我然后使用了简单的CSS隐藏包含类别字段的表行,因此您看到的只是复选框,就像:

alt text

现在,我可以将类别添加到图像附件中,我可以使用以下内容:

get_posts('post_type=attachment&category_name=timber-fixed-windows')

并将分类图像拉入页面!正是我希望做的事情,我认为没有一种方法可以做到这一点,但很高兴我设法弄清楚了一些事情。

我把它变成了一个叫做的插件 WOS Media Categories 我可以从中下载 我的网站suburbia.org.uk, ,我希望这可能对别人有用!再次感谢那些在这里提出的问题和其他问题的人,这有助于弄清楚这一点!

更新:我添加了一个修复程序,以启用类别,同时使用Flash Bulk上传器上传图像。

其他提示

刚刚创建了这个,这是Herky-Jerk JavaScript链接到表单字段的完整解决方法。由于您的复选框的值与提交的$ _ post一起传递,因此您只需在add_image_attachment_fields_to_to_save filter中抓取它们,并设置邮政对象的条款即可。

function register_custom_taxonomies() {
    $labels = array(
        'name' => _x( 'Image Formats', 'taxonomy general name' ),
        'singular_name' => _x( 'Image Format', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Formats' ),
        'all_items' => __( 'All Formats' ),
        'parent_item' => __( 'Parent Format' ),
        'parent_item_colon' => __( 'Parent Format:' ),
        'edit_item' => __( 'Edit Format' ), 
        'update_item' => __( 'Update Format' ),
        'add_new_item' => __( 'Add New Format' ),
        'new_item_name' => __( 'New Format Name' ),
        'menu_name' => __( 'Image Format' )
    );
    $capabilities = array(
        'manage_terms' => 'nobody',
        'edit_terms' => 'nobody',
        'delete_terms' => 'nobody'
    );
    $args = array(
        'public' => false,
        'hierarchical' => true,
        'labels' => $labels,
        'capabilities' => $capabilities,
        'show_ui' => false,
        'query_var' => 'image-format',
        'rewrite' => false
    );
    register_taxonomy('image-format', array('attachment'), $args);
}
add_action( 'init', 'register_custom_taxonomies', 1);

function add_media_categories($fields, $post) {
    $categories = get_categories(array('taxonomy' => 'image-format', 'hide_empty' => 0));
    $post_categories = wp_get_object_terms($post->ID, 'image-format', array('fields' => 'ids'));
    $all_cats .= '<ul id="media-categories-list" style="width:500px;">'; 
    foreach ($categories as $category) {
        if (in_array($category->term_id, $post_categories)) {
            $checked = ' checked="checked"';
        } else {
            $checked = '';  
        }
        $option = '<li style="width:240px;float:left;"><input type="checkbox" value="'.$category->category_nicename.'" id="'.$post->ID.'-'.$category->category_nicename.'" name="'.$post->ID.'-'.$category->category_nicename.'"'.$checked.'> ';
        $option .= '<label for="'.$post->ID.'-'.$category->category_nicename.'">'.$category->cat_name.'</label>';
        $option .= '</li>';
        $all_cats .= $option;
    }
    $all_cats .= '</ul>';

    $categories = array('all_categories' => array (
            'label' => __('Image Formats'),
            'input' => 'html',
            'html' => $all_cats
    ));
    return array_merge($fields, $categories);
}
add_filter('attachment_fields_to_edit', 'add_media_categories', null, 2);

function add_image_attachment_fields_to_save($post, $attachment) {
    $categories = get_categories(array('taxonomy' => 'image-format', 'hide_empty' => 0));
    $terms = array();
    foreach($categories as $category) {
        if (isset($_POST[$post['ID'].'-'.$category->category_nicename])) {
            $terms[] = $_POST[$post['ID'].'-'.$category->category_nicename];        
        }
    }
    wp_set_object_terms( $post['ID'], $terms, 'image-format' );
    return $post;
}
add_filter('attachment_fields_to_save', 'add_image_attachment_fields_to_save', null , 2);

(请注意,我使用的是自定义分类法,而不是类别,因此您必须更改$类别数组以匹配与设置复选框时使用的相同数组)

Shabam,Shabozzle。享受。

如果要使用WordPress的默认类别框,这将很困难/复杂。一方面,Metabox不会返回输出,它只是回荡。最重要的是,它不会为您提供正确的输入字段名称,因此无法保存。一个想法可能是使用 jQuery UI自动完成 复制标签框的功能。

但是,如果您想与媒体编辑的字段一起玩耍,则可以挂接 'attachment_fields_to_edit' 并编辑字段数组。过滤器将两个参数传递给回调:第一个参数是字段数组,其次是附件帖子对象。请参阅此处以获取更多详细信息:

http://phpxref.ftwr.co.uk/wordpress/nav.html?wp-admin/includes/media.php.source.html#l1025

我已经使用 @rickcurran的WOS媒体类别创建了一个插件作为起点。但是,WOS媒体类别与其他为媒体添加类别支持的插件一样,实际上并没有添加Metabox,我只是做到了。

overall view

它必须简化帖子和页面上的metaboxes,但我确实包括过滤能力,使其易于使用。

filterable categories

实际上,我正在生成您在页面和帖子上看到的整个类别Metabox,但是由于缺乏样式和缺失的JavaScript,隐藏在媒体页面上不起作用的位。

我欢迎任何人可能对如何使Metabox充分发挥功能的任何想法 - 我打算在以后的版本中做这件事。

很棒的插件Rick-非常有帮助。

如果您将OnClick Trigger内联移动而不是将其绑定到onload(并进行其他一些小调整),则它也将在闪存散装上载器上工作。使用当前版本,jQuery加载事件之后,Flash加载了,因此对象尚不存在。

修订JS:

 function wos_category_click(cat){
    var container = jQuery(cat).closest("tbody");
    var cat_checked = jQuery(container).find("tr.all_categories input:checked");
    var cat_arr = jQuery(cat_checked).map(function() {
        return jQuery(this).val();
    }).get().join();
    jQuery(container).find("tr.category > td.field > input.text").val(cat_arr);
}

将ONCLICK添加到PHP文件中的输入:

<input type="checkbox" onclick="wos_category_click(this)" class="wos-categories-cb"....

将批量上传器表单ID添加到CSS文件:

form#media-single-form tr.category,form#file-form tr.category {
display:none; 

}

许可以下: CC-BY-SA归因
scroll top