The PHP function of adding categories and tags to media files in WordPress ,. The PHP function of adding categories and tags to media files in WordPress is implemented. The media files uploaded from the WordPress background media library do not have to be assigned categories and tags as the document does, however, WordPress adds the PHP function of classification and tag to media files,
Media files uploaded from the WordPress background media library do not have to be classified and tagged as articles do. However, many times we need such features, such as download sites and image sites.
Original status of the media editing page
Obviously, on the WordPress background media editing page, no category or tag is selected by default.
Add a category to a media file
Add the following php code to 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, and you will see an additional category directory on the right bar:
Add tags to media files
Add the following code to 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 and you will see an additional tab bar on the right bar:
At the same time, on the multimedia list page, you can also add categories and tags for the displayed Media. in the same way, two sub-menus, category directory and tag, are added under the multimedia menu on the left bar:
Now, adding categories and tags to media files is as simple as this. we can combine the code added above into the following code, which is more concise and 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 method
In fact, there is another way to achieve this, and you can add independent categories for multimedia without mixing the classification of articles. Moreover, you can intuitively display the classification of media on the multimedia list page of the WordPress background.
Add the php code to the functions. php file of the current topic:
Function ludou_create_media_category () {$ args = array ('label' => 'Media category', '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, we recommend this plug-in: Media Library Categories, which can be used to modify the classification of Media files in batches.
Articles you may be interested in:
- WordPress restricts non-administrator users to comment only once after the article
- Describes the PHP functions used to create and add filters in WordPress.
- PHP functions used to create and retrieve sidebar in WordPress
Secret: a media file uploaded from the WordPress background media library. unlike an article, you can specify a category and a tag for it, but it is very...