天天看点

如何在WordPress中的自定义分类法中添加自定义元字段

By default WordPress taxonomies (categories, tags, etc) have the fields name, slug, parent, and description. Recently while working on a client’s project, we found a need to add custom meta fields to custom taxonomies. We needed a way to add custom text on each taxonomy archive page. One way would be to hard code the text using the conditional statements in our taxonomy-{name}.php file. That would be a very inefficient way of doing so, and it would give our client no way to modify the text in the future. So we decided to future proof the situation by adding custom meta fields to custom taxonomies. In this article, we will show you how to add additional custom meta fields to custom taxonomies.

默认情况下,WordPress分类法(类别,标签等)具有字段名称,子词,父级和描述。 最近,在处理客户的项目时,我们发现需要在自定义分类法中添加自定义元字段。 我们需要一种在每个分类存档页面上添加自定义文本的方法。 一种方法是使用我们的分类法-{name} .php文件中的条件语句对文本进行硬编码。 这将是一种非常低效的方式,并且将来我们的客户也无法修改文本。 因此,我们决定通过在自定义分类法中添加自定义元字段来证明这种情况。 在本文中,我们将向您展示如何向自定义分类法中添加其他自定义元字段。

Note: This tutorial is for designers and developers.

注意:本教程适用于设计人员和开发人员。

While searching for an efficient method, we came across Pippin’s tutorial that shows you how to do this. While his tutorial was great, it required us to write a lot of code. We decided to search a little bit further to see if someone has created an easier way to do this. Perhaps a plugin or a class. Thankfully, we found a solution by Ohad Raz on Github. After going through the same issue, he decided to write a class to make it easy for everyone else (got to love the WordPress community). Thanks Ohad.

在寻找有效的方法时,我们遇到了Pippin的教程 ,该教程向您展示了如何执行此操作。 尽管他的教程很棒,但它要求我们编写很多代码。 我们决定进一步搜索,以了解是否有人创建了一种更简单的方法来执行此操作。 也许是插件或类。 值得庆幸的是,我们在Github上找到了Ohad Raz的解决方案。 在经历了同样的问题之后,他决定编写一门课,以使其他所有人都容易上手(请爱上WordPress社区)。 谢谢奥哈德。

In our case, we decided to add this functionality as a plugin rather than in a theme. You can choose the method you like. For the sake of this tutorial, we will go the plugin route.

在我们的案例中,我们决定将此功能添加为插件而不是主题。 您可以选择自己喜欢的方法。 为了本教程的缘故,我们将走插件路线。

First thing you need to do is download the Tax-Meta-Class from Github. Create a new folder and call it “taxonomy-fields”. Save the “Tax-meta-class” folder inside that folder.

您需要做的第一件事是从Github下载Tax-Meta-Class 。 创建一个新文件夹,并将其命名为“ taxonomy-fields”。 将“ Tax-meta-class”文件夹保存在该文件夹中。

The zip comes with a file called class-usage-demo.php. Just rename that file, and call it taxonomy-fields.php

该zip随附一个名为class-usage-demo.php的文件。 只需重命名该文件,并将其命名为taxonomy-fields.php

Ohad did a great job in documenting the file, so it is pretty self-explanatory. He has examples of all type of fields you can add (text field, textarea, checkbox, select, radio, date, time, color picker, file upload, etc). You don’t have to use all the fields. Simply get rid of the ones you don’t want.

Ohad在记录文件方面做得很出色,因此非常不言自明。 他提供了您可以添加的所有类型的字段的示例(文本字段,文本区域,复选框,选择,单选,日期,时间,颜色选择器,文件上传等)。 您不必使用所有字段。 只需摆脱不想要的那些。

Once you are done adding the fields, upload the taxonomy-fields folder in your plugins folder. Activate the plugin, and add data in your fields.

添加完字段后,在您的plugins文件夹中上传taxonomy-fields文件夹。 激活插件,然后在您的字段中添加数据。

Now, you are ready to display these additional fields in your taxonomy template. Open your taxonomy template. This would be something like taxonomy-{taxonomy-name}.php file. In there, you can simply add the following:

现在,您可以在分类模板中显示这些其他字段。 打开分类模板。 这将类似于taxonomy- {taxonomy-name} .php文件。 您可以在其中简单添加以下内容:

<?php 

//Get the correct taxonomy ID by slug
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

//Get Taxonomy Meta
$saved_data = get_tax_meta($term->term_id,'text_field_id');
echo $saved_data; 

?>
           

That’s it. These classes make it really easy and improve your workflow. We hope that this tutorial has helped you in adding custom meta fields to custom taxonomies.

而已。 这些类非常简单,可以改善您的工作流程。 我们希望本教程能够帮助您将自定义元字段添加到自定义分类法中。

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-add-additional-custom-meta-fields-to-custom-taxonomies/