Term meta containers用于使用附加字段扩展term编辑屏幕。 使用 WordPress 4.4 及更高版本的默认术语元功能,为每个term单独存储字段数据。 对于 4.4 之前的 WordPress 版本,数据存储在自定义表中 ($wpdb->termmeta
)。
use Carbon_Fields\Container;
use Carbon_Fields\Field;
Container::make( 'term_meta', __( 'Category Properties' ) )
->where( 'term_taxonomy', '=', 'category' )
->add_fields( array(
Field::make( 'color', 'crb_title_color', __( 'Title Color' ) ),
Field::make( 'image', 'crb_thumb', __( 'Thumbnail' ) ),
) );
访问字段值
要访问字段值,您需要使用函数 carbon_get_term_meta( $term_id, $name )
,其中:
参数 | 描述 |
---|---|
$term_id |
输入值的Term ID。 |
$name |
要检索的字段的字段名称模式。 |
<!-- Simple field -->
<p>Editor of this category: <?php echo carbon_get_term_meta( $category->term_id, 'crb_editor' ); ?></p>
<!-- Complex field -->
<?php
$authors = carbon_get_term_meta( $category->term_id, 'crb_authors' );
foreach ( $authors as $author ) {
echo $author['name'];
}
?>
保存后,会调用钩子,它允许您在保存后钩子附加功能。 它接受参数,这是更新的taxonomy term的。carbon_fields_term_meta_container_saved $term_idterm_id