我已经pa_color
通过以下自然方式为分类术语创建了一个“颜色”自定义字段:
这是我的代码:
add_action( 'pa_color_add_form_fields', 'themename_pa_color_add_term_fields' );
function themename_pa_color_add_term_fields( $taxonomy ) {
?>
<div class="form-field">
<label for="themename_pa_color">PA Color</label>
<input type="color" name="themename_pa_color" id="themename_pa_color" class="wpColorChoose" />
<p>Field description may go here.</p>
</div>
<?php
}
add_action( 'pa_color_edit_form_fields', 'themename_pa_color_edit_term_fields', 10, 2 );
function themename_pa_color_edit_term_fields( $term, $taxonomy ) {
$value = get_term_meta( $term->term_id, 'themename_pa_color', true );
echo '<tr class="form-field">
<th>
<label for="themename_pa_color">PA Color</label>
</th>
<td>
<input name="themename_pa_color" id="themename_pa_color" type="color" class="wpColorChoose" value="' . esc_attr( $value ) .'" />
<p class="description">Field description may go here.</p>
</td>
</tr>';
}
add_action( 'created_pa_color', 'themename_pa_color_save_term_fields' );
add_action( 'edited_pa_color', 'themename_pa_color_save_term_fields' );
function themename_pa_color_save_term_fields( $term_id ) {
update_term_meta(
$term_id,
'themename_pa_color',
sanitize_text_field( $_POST[ 'themename_pa_color' ] )
);
}
我怎样才能将他们的价值观带到我需要的地方?
尝试使用时$term->themename_pa_color
,它不起作用。
它适用于名称和描述默认字段,例如$term->name
或$term->description
,但不适用于我的字段。
这就是我创建它的方式,它可以正确保存值。
您的代码正在为“ pa_color”分类法中的术语添加自定义字段。因此,这是关于术语元数据的,它从未包含在WP_Term
对象中,因此不能作为该WP_Term
对象的属性来访问。
因此答案就在您的代码中。您需要get_term_meta()
像在自己的代码中一样(在第二个函数中)使用WordPress函数:
$value = get_term_meta( $term->term_id, 'themename_pa_color', true );
echo $value; // Display value
($term->term_id
术语Id和themename_pa_color
meta键在哪里) …
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句