問題描述
我正在使用 register_taxonomy_for_object_type()將類別分類欄位新增到媒體上傳 (附件) 。我正在使用這個程式碼:
add_action('init', 'reg_tax');
function reg_tax() {
register_taxonomy_for_object_type('category', 'attachment');
}
這可以在檢視影像時向 Media 頁面新增類別的簡單文字欄位。我真正想要的是讓它顯示實際的類別 Metabox,以便我可以選擇我想使用的類別,而不是將它們輸入到平原欄位。我也發現,把這個類別的外掛放到這樣的文字欄位中,比如 my-category-name,當它被儲存時,最終會顯示為實際的類別名稱,像我的類別名稱,這使得簡單的文字欄位更少是一個有用的選項。
我一直在看 add_post_type_support()功能新增 Metaboxes,並已經看到它用於自定義帖子型別,我只是看不到可以新增相同的附件。
最佳解決方案
我會在這裡回答我自己的問題,因為我已經設法找出了我一直在努力做的一個解決方案。我得出的結論是,不可能得到附件的類別 Metabox 。但是,我發現使用 register_taxonomy_for_object_type 和 add_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');
}
新增的欄位如下所示:
它只是一個純文字欄位,但是我發現可以在其中鍵入現有類別的名稱,然後在附件被更新時成功儲存 (唯一奇怪的行為是它渲染回正常版本而不是儲存後的 s 。) 。
一旦我意識到我可以這樣儲存類別,那麼我想我可以得到所有可用類別的列表作為核取方塊,並檢查已被選擇的類別。然後,我使用一些 jQuery 來抓取已檢查類別的值,並將所有類別的外掛放入 「類別」 欄位。為了使這看起來更加無縫,我然後使用一些簡單的 CSS 來隱藏包含 「類別」 欄位的錶行,所以你看到的所有核取方塊就像這樣:
現在我可以新增類別到影像附件我可以使用像:
get_posts('post_type=attachment&category_name=timber-fixed-windows')
並將分類的影像拉入頁面!正是我希望做的,我不認為會有辦法,但很高興我設法弄清楚一些事情。
我已經把它變成一個名為 WOS Media Categories 的外掛,我已經可以從 my website, Suburbia.org.uk 下載,希望它可能對其他人有用!再次感謝那些對這個和其他問題發表了意見的人,這些問題幫助瞭解了!
更新:我已經新增了修復程式,以便在使用 Flash 批次上傳器上傳影像時啟用類別。
次佳解決方案
剛剛建立了這個,這是一個完整的解決方法,herky-jerk javascript 連結到表單域。由於您的核取方塊的值與 $ _POST 一起提交,您可以在 add_image_attachment_fields_to_save 過濾器中抓取它們,並設定後期物件的條款。
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);
(請注意,我使用自定義分類法,而不是類別,因此您必須更改 $ categories 陣列以匹配您設定核取方塊時使用的陣列)
沙巴姆,沙巴。請享用。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。

