与文章不同,从WordPress后台媒体库上传的媒体文件可以分配类别和标签,但我们经常需要这样的功能,比如一些下载站和照片站。
编辑媒体页面的原始状态。
很明显,在WordPress后台媒体编辑页面,默认没有分类和标签供你选择。
向媒体文件添加分类。
将以下php代码添加到当前主题的functions.php中:
function ludou _ add _ categories _ to _ attachments(){ register _ taxonomy _ for _ object _ type(' category ',' attachment ');}add_action('init ',' ludou _ add _ categories _ to _ attachments ');保存并上传functions.php,刷新媒体编辑页面,您会看到右栏中有一个类别:
向媒体文件添加标签。
将以下代码添加到当前主题的functions.php:
function ludou _ add _ tags _ to _ attachments(){ register _ taxonomy _ for _ object _ type(' post _ tag ',' attachment ');}add_action('init ',' ludou _ add _ tags _ to _ attachments ');保存并上传functions.php,刷新媒体编辑页面,您会看到右栏中有一个标签栏:
同时,在多媒体列表页面中,还添加了显示媒体的类别和标签。同样,在左栏的多媒体菜单下,有两个子菜单:分类目录和标签:
好吧,这就像给媒体文件添加类别和标签一样简单。我们可以将上面添加的代码合成为下面的代码,这样更简洁高效:
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 ');另一种方法
其实还有一种方法,可以在不混合文章分类的情况下,给多媒体添加独立的分类,WordPress后台的多媒体列表页面可以直观的显示媒体的分类。
使用方法,您还可以将php代码添加到当前主题的functions.php:
函数ludou _ create _ media _ category(){ $ args=array(' label '=' media classification ',' hierarchical '=true,' show _ admin _ column'=true,' show _ ui'=true,' query _ var'=true,'重写'。register _分类学(' attachment_category ',' attachment ',$ args);}add_action('init ',' ludou _ create _ media _ category ');如果不排除使用插件,推荐这个插件:媒体库类别,可以批量修改媒体文件的分类。