WordPress to add categories and tags to the media file PHP function implementation,
From the WordPress background Media Library uploaded media files, not like the article can give it to assign categories and tags, but many times we need such features, such as some download stations, picture stations and so on.
The original state of the media editing page
Obviously, in the WordPress background Media editing page, by default, there is no classification and tags for you to choose.
Add 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 editing page, you will see the right column more than a category directory:
Add tags to 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 functions.php, refresh the media editing page, you will see the right column more than a tab bar:
At the same time, in the Multimedia list page, also add the display media classification and labels, the same left column under the Multimedia menu also has the category directory and the label two sub-menu:
Well, adding categories and tags to media files is as simple as that, 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 ');
A different approach
In fact, there is a way to achieve, and can be implemented to add independent classification of multimedia, without mixing the classification of articles, and in the WordPress background of the Multimedia list page can be visualized to display the media classification.
Using the same method, add the PHP code to the functions.php of the current topic:
function Ludou_create_media_category () {$args = array ( ' label ' = ' = ' Media class ', ' 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 exclude the use of plug-ins, recommend this plugin: Media Library Categories, can be implemented in bulk to modify the classification of media files.
Articles you may be interested in:
- WordPress restricts non-admin users to comment only once after the article
- Detailed PHP functions for creating and adding filters in WordPress
- PHP functions for creating and getting sidebar in WordPress
http://www.bkjia.com/PHPjc/1087273.html www.bkjia.com true http://www.bkjia.com/PHPjc/1087273.html techarticle WordPress in the media files to add categories and tags of the PHP function implementation, from the WordPress background Media Library upload media files, not like the article can give it to assign categories and tags, but very ...