为wordpress媒体文件添加分类目录和标签的方法

  • 时间:2020-05-22 15:47:15
  • 分类:网络文摘
  • 阅读:162 次

在wordpress中,我们可以为文章指定分类目录和添加标签以便于管理。但对于媒体文件,wordpress默认却没有这样的功能。那么,有没有办法为图片、附件或视频等媒体文件也可以指定分类目录和标签呢?答案是肯定的,请往下看。

1、为媒体文件添加分类目录和标签

添加如下代码到wordpress主题的functions.php文件中。

  1. function ludou_add_categories_tags_to_attachments() {
  2.    register_taxonomy_for_object_type( 'category', 'attachment' );
  3.    register_taxonomy_for_object_type( 'post_tag', 'attachment' );
  4. }
  5. add_action( 'init' , 'ludou_add_categories_tags_to_attachments' );

之后,可以看到在仪表盘右侧的多媒体菜单中,多了“分类目录”和“标签”两个选项。

为wordpress媒体文件添加分类目录和标签的方法

再打开媒体编辑页面,就可以为媒体文件指定分类目录和添加标签了。

为wordpress媒体文件添加分类目录和标签的代码

同时,写文章插入多媒体时,也可以直接为媒体文件指定分类目录和添加标签。

WordPress让媒体文件也有分类和标签

此外,在媒体文件列表中也会显示媒体文件所在的分类目录和标签。

WordPress让媒体文件也有分类和标签

2、只为媒体文件添加分类目录

如果只想添加分类目录,可以复制下面的代码到functions.php文件中:

  1. function ludou_add_categories_to_attachments() {
  2.    register_taxonomy_for_object_type( 'category', 'attachment' );
  3. }
  4. add_action( 'init' , 'ludou_add_categories_to_attachments' );

3、只为媒体文件添加标签

如果只想添加标签,可以复制下面的代码到functions.php文件中:

  1. function ludou_add_tags_to_attachments() {
  2.    register_taxonomy_for_object_type( 'post_tag', 'attachment' );
  3. }
  4. add_action( 'init' , 'ludou_add_tags_to_attachments' );

好了,通过上面的wordpress技巧,已经可以实现本文开头提出的问题了。一般而言,此方法对于用wordpress做下载站或图片站的朋友或许更有帮助。通过为媒体文件指定分类目录和添加标签,可以使网站的内容更有条理、脉络更加清晰,管理也会更加方便。而对于普通博客的作用并不会十分明显,如何取舍,就看你的个人需要了。

代码出自:http://www.ludou.org/wordpress-applying-categories-tags-and-custom-taxonomies-to-media-attachments.html

推荐阅读:
How to Check If Two Strings are Buddy Strings?  Innovative Plugins You Need On Your Mobile Business Site  Why Having a Phone Service is a Must for Businesses?  How to Validate a Perfect Number (Integer)?  Binary Prefix Divisible By 5 – Java/C++ Coding Exercise  The DNS Lookup Tool in Java (InetAddress)  How to Check if a Matrix is a Toeplitz Matrix?  How to Uncommon Words from Two Sentences in Java?  The Best Bootable USB Creation Tool for Windows, Linux and Mac  How to Compute the Number Complement for Integers? 
评论列表
添加评论