要获得所有工作,您需要将任期元数据作为每个数据的WP_OPTIONS表中的字段保存为字段,这意味着您可能有数百个,如果不是数千个额外的条目,则术语或仅几个定制分类法。
但是,由于WordPress 4.4及以后,术语现在为对象,与帖子,页面和自定义内容类型相同。此更改使添加,删除和更新您的元数据变得更加容易。>
带有术语meta的背景故事
自从WordPress 2.8返回以来,社区一直在推动一种简单的方法来控制术语元数据。这是一个缓慢的过程,但最终从头开始重新设计了术语以使用班级结构。这加上WordPress 4.4的一些不同的更改意味着分类法(例如“标签”,“类别”或“自定义”)中的术语现在可以轻松地分配给他们。在WordPress 4.4之前,没有明确的方法可以轻松地为术语项目保存元数据,这是对构造方式的术语的固有限制。如果您要扩展分类法或条款,则必须将数据直接保存为使用Update_option的站点选项。 这不是理想的(因为它使选项表杂乱无章)。
>//saving new fields for category function save_extra_taxonomy_fields($term_id){ $term = get_term($term_id); $term_slug = $term->slug; //collect category image id from posted values $term_category_image_id = isset($_POST['category_image_id']) ? sanitize_text_field($_POST['category_image_id']) : ''; //update value and save it as an option update_option('category_image_id_' . $term_slug, $term_category_image_id); } add_action('create_category','save_extra_taxonomy_fields');
添加,更新和删除术语元
添加术语元
读取术语meta
function add_featured_to_categories(){ //get all terms from the category taxonomy $taxonomy_name = 'category'; $term_args = array( 'orderby' => 'name', 'hide_empty' => false, 'fields' => 'ids' ); $terms = get_terms($taxonomy_name, $term_args); if($terms){ $term_key = 'term_size'; $term_value = 'empty'; $term_unique = true; //go through all terms and set the new term meta foreach($terms as $term_id){ $term = get_term($term_id, $taxonomy_name); $term_count = $term->count; //determine new meta value if($term_count > 10){ $term_value = 'big'; }else if($term_count >= 5 && $term_count < 10){ $term_value = 'medium'; }else if($term_count >= 1 && $term_count < 5){ $term_value = 'small'; } //save meta value add_term_meta($term_id, $term_key, $term_value, $term_unique); } } } add_action('init', 'add_featured_to_categories');
>获取元数据的术语ID
$键(可选) - 您要返回的单个指定密钥。如果未指定,则将返回所有元数据。现在,在我们的类别中,或其他儿童主题模板文件,我们可以修改显示我们的术语数据的功能。
在我的情况下,我正在使用二十四个问题进行编辑类别。
//given a term, collect its saved image to be displayed function display_term_meta_image($term_id, $term_taxonomy){ //get supplied term $term = get_term($term_id, $term_taxonomy); if($term){ $term_image_id = get_term_meta($term_id, 'term_image_id', true); if($term_image_id){ //get the medium image size for display $term_image = wp_get_attachment_image_src($term_image_id, 'medium', false); echo '<img . $term_image[0] . '" title="' . $term->name . ' image"/>'; } } }
>
>我们可以删除术语元数据,与帖子相同。当我们使用delete_term_meta函数时,如果需要,我们需要提供两个强制性参数。
此功能将通过并删除我们在$ wasse_term_keys变量中未指定的任何其他元数据,从而减少了数据库中浪费的空间(对于我们有数十个元数据条目时,我们不再需要)。 > 与WordPress 4.3及以上的
//saving new fields for category function save_extra_taxonomy_fields($term_id){ $term = get_term($term_id); $term_slug = $term->slug; //collect category image id from posted values $term_category_image_id = isset($_POST['category_image_id']) ? sanitize_text_field($_POST['category_image_id']) : ''; //update value and save it as an option update_option('category_image_id_' . $term_slug, $term_category_image_id); } add_action('create_category','save_extra_taxonomy_fields');
>您真的很想向前推进这些新的元功能,但想掩盖自己的旧版本,则可以创建一些有条件的功能以确保一切都可以。
>如果我们没有支持,我们会抓住术语对象本身(通过术语ID中的术语),然后我们提取$ TERM_TAXONOMY数据并使用它来构建我们的最终键值。由于我们将保存到选项表中,我们需要确保密钥是唯一的,因此我们通过将分类法名称,术语ID和术语键添加到一个变量中来做到这一点。 我们必须确保密钥的长度不超过64个字符,并将其修剪成。完成所有这些操作后,我们可以调用我们的add_option函数以节省我们的价值。
function add_featured_to_categories(){ //get all terms from the category taxonomy $taxonomy_name = 'category'; $term_args = array( 'orderby' => 'name', 'hide_empty' => false, 'fields' => 'ids' ); $terms = get_terms($taxonomy_name, $term_args); if($terms){ $term_key = 'term_size'; $term_value = 'empty'; $term_unique = true; //go through all terms and set the new term meta foreach($terms as $term_id){ $term = get_term($term_id, $taxonomy_name); $term_count = $term->count; //determine new meta value if($term_count > 10){ $term_value = 'big'; }else if($term_count >= 5 && $term_count < 10){ $term_value = 'medium'; }else if($term_count >= 1 && $term_count < 5){ $term_value = 'small'; } //save meta value add_term_meta($term_id, $term_key, $term_value, $term_unique); } } } add_action('init', 'add_featured_to_categories');
将其全部包装
使用这些新的元功能应使您更轻松地扩展您的术语以提供独特的功能。例如,您可能需要在术语的顶部添加横幅图像或提供元数据,以便您可以有条件地以不同的方式显示您的条款(例如,根据显示的术语加载新的模板文件)。
>具有新术语元函数的灵活性和易度性,您可以立即开始在新项目中实现此功能!>
WordPress项META是一项功能,它允许您将自定义元数据存储在WordPress站点中。此元数据可以是您想与该术语相关联的其他描述,图像,颜色或任何其他信息。这是一个功能强大的工具,可以增强网站的功能和用户体验。
在WordPress中添加术语META,您可以使用add_term_meta( ) 功能。此功能采用三个必需的参数:术语ID,元密钥(元字段的名称)和元值。可选地,您还可以指定元密钥是否应该是唯一的。
>>您可以使用get_term_meta()function在WordPress中检索术语元数据。此功能需要您要检索的元字段的术语ID和键。它返回指定术语的元字段的值。
是的,您可以使用update_term_meta()function在WordPress中更新术语Meta。此功能需要术语ID,元密钥和新的元值。如果该术语不存在元密钥,则将添加。
>> WordPress中的术语元素可用于多种目的,例如将其他信息添加到类别或标签中,为术语创建自定义字段,增强搜索功能等等。我可以使用WordPress中的自定义分类法的术语元素吗?术语元函数可与任何分类法(包括自定义的分类法)一起使用。
>
以上是WordPress术语元和WP_TERM简介的详细内容。更多信息请关注PHP中文网其他相关文章!