莫讓我說恁是腦殘謝謝 發表於 2023-6-16 00:00:00

WordPress中给媒体文件添加分类和标签的PHP功能实现

<p>
从wordpress后台媒体库上传的媒体文件,不像文章那样可以给它指定分类和标签,但是很多时候我们又需要这样的功能,如一些下载站、图片站等。</p>
<p>
媒体编辑页面的原始状态</p>
<p>
<img style="max-width:100%!important;height:auto!important;"title="WordPress中给媒体文件添加分类和标签的PHP功能实现" alt="WordPress中给媒体文件添加分类和标签的PHP功能实现" src="https://zhuji.jb51.net/uploads/img/202305/7c910f675ad84b226cb8b4e60e9668c4.jpg"></p>
<p>
很明显,在wordpress后台的媒体编辑页面,默认情况下是没有分类和标签给你选的。</p>
<p>
<strong>给媒体文件添加分类</strong></p>
<p>
在当前主题的functions.php中添加以下php代码:</p>
<div>
<div>
<div id="highlighter_91707">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
<div>
4</div>
</td>
<td>
<div>
<div>
<code>function</code> <code>ludou_add_categories_to_attachments() {</code>
</div>
<div>
<code>  </code><code>register_taxonomy_for_object_type( </code><code>'category'</code><code>, </code><code>'attachment'</code> <code>);</code>
</div>
<div>
<code>}</code>
</div>
<div>
<code>add_action( </code><code>'init'</code> <code>, </code><code>'ludou_add_categories_to_attachments'</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
</div>
<p>
保存并上传functions.php,刷新一下媒体编辑页面,你会看到右边栏多了个分类目录:</p>
<p>
<img style="max-width:100%!important;height:auto!important;"title="WordPress中给媒体文件添加分类和标签的PHP功能实现" alt="WordPress中给媒体文件添加分类和标签的PHP功能实现" src="https://zhuji.jb51.net/uploads/img/202305/3891883542f687062dbb4207147a10c7.jpg"></p>
<p>
<strong>给媒体文件添加标签</strong></p>
<p>
在当前主题的functions.php中添加以下代码:</p>
<div>
<div>
<div id="highlighter_582308">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
<div>
4</div>
</td>
<td>
<div>
<div>
<code>function</code> <code>ludou_add_tags_to_attachments() {</code>
</div>
<div>
<code>  </code><code>register_taxonomy_for_object_type( </code><code>'post_tag'</code><code>, </code><code>'attachment'</code> <code>);</code>
</div>
<div>
<code>}</code>
</div>
<div>
<code>add_action( </code><code>'init'</code> <code>, </code><code>'ludou_add_tags_to_attachments'</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
</div>
<p>
保存并上传functions.php,刷新一下媒体编辑页面,你会看到右边栏多了个标签栏:</p>
<p>
<img style="max-width:100%!important;height:auto!important;"title="WordPress中给媒体文件添加分类和标签的PHP功能实现" alt="WordPress中给媒体文件添加分类和标签的PHP功能实现" src="https://zhuji.jb51.net/uploads/img/202305/dbe7a6edf783d832f5112a73198cadd3.jpg"></p>
<p>
同时,在多媒体列表页,也添加显示媒体的分类和标签,同样左边栏的多媒体菜单下也多了分类目录和标签两个子菜单:</p>
<p>
<img style="max-width:100%!important;height:auto!important;"title="WordPress中给媒体文件添加分类和标签的PHP功能实现" alt="WordPress中给媒体文件添加分类和标签的PHP功能实现" src="https://zhuji.jb51.net/uploads/img/202305/a835f60669c8dfe115b79c4bf1d372be.jpg"></p>
<p>
好了,给媒体文件添加分类和标签就这么简单,我们可以将上面添加的代码合成下面的代码,更简洁更高效:</p>
<div>
<div>
<div id="highlighter_375572">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
<div>
4</div>
<div>
5</div>
</td>
<td>
<div>
<div>
<code>function</code> <code>ludou_add_categories_tags_to_attachments() {</code>
</div>
<div>
<code>  </code><code>register_taxonomy_for_object_type( </code><code>'category'</code><code>, </code><code>'attachment'</code> <code>);</code>
</div>
<div>
<code>  </code><code>register_taxonomy_for_object_type( </code><code>'post_tag'</code><code>, </code><code>'attachment'</code> <code>);</code>
</div>
<div>
<code>}</code>
</div>
<div>
<code>add_action( </code><code>'init'</code> <code>, </code><code>'ludou_add_categories_tags_to_attachments'</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
</div>
<p>
<strong>另一种方法</strong></p>
<p>
其实还有一种方法实现,并且可以实现给多媒体添加独立的分类,而不必混用文章的分类,而且在wordpress后台的多媒体列表页可以直观显示媒体的分类。</p>
<p>
使用方法,同样是在当前主题的functions.php中添加php代码:</p>
<div>
<div>
<div id="highlighter_429504">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
<div>
4</div>
<div>
5</div>
<div>
6</div>
<div>
7</div>
<div>
8</div>
<div>
9</div>
<div>
10</div>
<div>
11</div>
<div>
12</div>
<div>
13</div>
<div>
14</div>
</td>
<td>
<div>
<div>
<code>function</code> <code>ludou_create_media_category() {</code>
</div>
<div>
<code> </code><code>$args</code> <code>= </code><code>array</code><code>(</code>
</div>
<div>
<code>  </code><code>'label'</code> <code>=&gt; </code><code>'媒体分类'</code><code>,</code>
</div>
<div>
<code>  </code><code>'hierarchical'</code> <code>=&gt; true,</code>
</div>
<div>
<code>  </code><code>'show_admin_column'</code> <code>=&gt; true,</code>
</div>
<div>
<code>  </code><code>'show_ui'</code>   <code>=&gt; true,</code>
</div>
<div>
<code>  </code><code>'query_var'</code>  <code>=&gt; true,</code>
</div>
<div>
<code>  </code><code>'rewrite'</code>   <code>=&gt; true,</code>
</div>
<div>
<code> </code><code>);</code>
</div>
<div>
 </div>
<div>
<code> </code><code>register_taxonomy( </code><code>'attachment_category'</code><code>, </code><code>'attachment'</code><code>, </code><code>$args</code> <code>);</code>
</div>
<div>
<code>}</code>
</div>
<div>
 </div>
<div>
<code>add_action( </code><code>'init'</code><code>, </code><code>'ludou_create_media_category'</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
</div>
<p>
如果你不排斥使用插件,推荐这个插件:media library categories,可以实现批量修改媒体文件的分类。</p>
頁: [1]
查看完整版本: WordPress中给媒体文件添加分类和标签的PHP功能实现