From the WordPress background Media Library upload media files, not like the article can give it to specify categories and tags, but many times we need such a function, such as some download stations, picture stations and so on.
The original state of the media edit page
Obviously, in the WordPress background of the media editing page, by default there is no classification and label for you to choose.
Adding categories to media files
Add the following PHP code to the functions.php of the current topic:
function ludou_add_categories_to_attachments () {
register_taxonomy_for_object_type (' category ', ' attachment ');
}
add_action (' init ', ' ludou_add_categories_to_attachments ');
Save and upload functions.php, refresh the media edit page, you will see the right column more than a category directory:
Tagging media files
Add the following code to the functions.php of the current topic:
function ludou_add_tags_to_attachments () {
register_taxonomy_for_object_type (' Post_tag ', ' attachment ');
}
Add_action (' init ', ' ludou_add_tags_to_attachments ');
Save and upload the functions.php, refresh the media editing page, you will see the right side of the bar more than a tab bar:
At the same time, in the Multimedia list page, also add the Display media category and label, also the left column of the multimedia menu under the Category directory and label two submenu:
Well, adding categories and labels to media files is simple, and we can synthesize the code added above to make it simpler and more efficient:
function ludou_add_categories_tags_to_attachments () {
register_taxonomy_for_object_type (' Category ', ' Attachment ');
Register_taxonomy_for_object_type (' Post_tag ', ' attachment ');
}
Add_action (' init ', ' ludou_add_categories_tags_to_attachments ');
Another way
In fact, there is a way to achieve, and can be implemented to add independent classification of multimedia, rather than mixing the classification of articles, and in the WordPress background of the Multimedia list page can visually display the classification of the media.
Using the method, you also add PHP code to the functions.php of the current topic:
function Ludou_create_media_category () {
$args = array (
' label ' => ' Media classification ', '
hierarchical ' => true,
' Show_admin_column ' => true,
' show_ui ' => true,
' Query_var ' => true,
' rewrite ' => True,
);
Register_taxonomy (' attachment_category ', ' attachment ', $args);
}
Add_action (' init ', ' ludou_create_media_category ');
If you do not reject the use of plug-ins, recommend this plugin: media Library Categories, you can achieve the bulk of the classification of the file changes.