我该如何为产品分类添加自定义字段?我已经为产品添加了自定义字段,但找不到任何扩展可以提供添加产品分类自定义字段的功能。
我该如何为产品分类添加自定义字段?我已经为产品添加了自定义字段,但找不到任何扩展可以提供添加产品分类自定义字段的功能。
4.4
及以上版本:update_term_meta()
和get_term_meta()
函数。这意味着现在我们不必再把数据保存在wp_options
表中,而是现在存储在wp_termmeta
表中。//Product Cat Create page
function wh_taxonomy_add_new_meta_field() {
?>
<div class="form-field">
<label for="wh_meta_title"><?php _e('Meta Title', 'wh'); ?></label>
<input type="text" name="wh_meta_title" id="wh_meta_title">
<p class="description"><?php _e('Enter a meta title, <= 60 character', 'wh'); ?></p>
</div>
<div class="form-field">
<label for="wh_meta_desc"><?php _e('Meta Description', 'wh'); ?></label>
<textarea name="wh_meta_desc" id="wh_meta_desc"></textarea>
<p class="description"><?php _e('Enter a meta description, <= 160 character', 'wh'); ?></p>
</div>
<?php
}
//Product Cat Edit page
function wh_taxonomy_edit_meta_field($term) {
//getting term ID
$term_id = $term->term_id;
// retrieve the existing value(s) for this meta field.
$wh_meta_title = get_term_meta($term_id, 'wh_meta_title', true);
$wh_meta_desc = get_term_meta($term_id, 'wh_meta_desc', true);
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="wh_meta_title"><?php _e('Meta Title', 'wh'); ?></label></th>
<td>
<input type="text" name="wh_meta_title" id="wh_meta_title" value="<?php echo esc_attr($wh_meta_title) ? esc_attr($wh_meta_title) : ''; ?>">
<p class="description"><?php _e('Enter a meta title, <= 60 character', 'wh'); ?></p>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="wh_meta_desc"><?php _e('Meta Description', 'wh'); ?></label></th>
<td>
<textarea name="wh_meta_desc" id="wh_meta_desc"><?php echo esc_attr($wh_meta_desc) ? esc_attr($wh_meta_desc) : ''; ?></textarea>
<p class="description"><?php _e('Enter a meta description', 'wh'); ?></p>
</td>
</tr>
<?php
}
add_action('product_cat_add_form_fields', 'wh_taxonomy_add_new_meta_field', 10, 1);
add_action('product_cat_edit_form_fields', 'wh_taxonomy_edit_meta_field', 10, 1);
// Save extra taxonomy fields callback function.
function wh_save_taxonomy_custom_meta($term_id) {
$wh_meta_title = filter_input(INPUT_POST, 'wh_meta_title');
$wh_meta_desc = filter_input(INPUT_POST, 'wh_meta_desc');
update_term_meta($term_id, 'wh_meta_title', $wh_meta_title);
update_term_meta($term_id, 'wh_meta_desc', $wh_meta_desc);
}
add_action('edited_product_cat', 'wh_save_taxonomy_custom_meta', 10, 1);
add_action('create_product_cat', 'wh_save_taxonomy_custom_meta', 10, 1);
使用方法, 检索数据:
echo $productCatMetaTitle = get_term_meta($term_id, 'wh_meta_title', true);
echo $productCatMetaDesc = get_term_meta($term_id, 'wh_meta_desc', true);
### WP版本低于`4.4`。
这是代码
//Product Cat creation page
function text_domain_taxonomy_add_new_meta_field() {
?>
<div class="form-field">
<label for="term_meta[wh_meta_title]"><?php _e('Meta Title', 'text_domain'); ?></label>
<input type="text" name="term_meta[wh_meta_title]" id="term_meta[wh_meta_title]">
<p class="description"><?php _e('Enter a meta title, <= 60 character', 'text_domain'); ?></p>
</div>
<div class="form-field">
<label for="term_meta[wh_meta_desc]"><?php _e('Meta Description', 'text_domain'); ?></label>
<textarea name="term_meta[wh_meta_desc]" id="term_meta[wh_meta_desc]"></textarea>
<p class="description"><?php _e('Enter a meta description, <= 160 character', 'text_domain'); ?></p>
</div>
<?php
}
add_action('product_cat_add_form_fields', 'text_domain_taxonomy_add_new_meta_field', 10, 2);
//Product Cat Edit page
function text_domain_taxonomy_edit_meta_field($term) {
//getting term ID
$term_id = $term->term_id;
// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option("taxonomy_" . $term_id);
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[wh_meta_title]"><?php _e('Meta Title', 'text_domain'); ?></label></th>
<td>
<input type="text" name="term_meta[wh_meta_title]" id="term_meta[wh_meta_title]" value="<?php echo esc_attr($term_meta['wh_meta_title']) ? esc_attr($term_meta['wh_meta_title']) : ''; ?>">
<p class="description"><?php _e('Enter a meta title, <= 60 character', 'text_domain'); ?></p>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[wh_meta_desc]"><?php _e('Meta Description', 'text_domain'); ?></label></th>
<td>
<textarea name="term_meta[wh_meta_desc]" id="term_meta[wh_meta_desc]"><?php echo esc_attr($term_meta['wh_meta_desc']) ? esc_attr($term_meta['wh_meta_title']) : ''; ?></textarea>
<p class="description"><?php _e('Enter a meta description', 'text_domain'); ?></p>
</td>
</tr>
<?php
}
add_action('product_cat_edit_form_fields', 'text_domain_taxonomy_edit_meta_field', 10, 2);
// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta($term_id) {
if (isset($_POST['term_meta'])) {
$term_meta = get_option("taxonomy_" . $term_id);
$cat_keys = array_keys($_POST['term_meta']);
foreach ($cat_keys as $key) {
if (isset($_POST['term_meta'][$key])) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
// Save the option array.
update_option("taxonomy_" . $term_id, $term_meta);
}
}
add_action('edited_product_cat', 'save_taxonomy_custom_meta', 10, 2);
add_action('create_product_cat', 'save_taxonomy_custom_meta', 10, 2);
使用方法,用于检索数据:
$metaArray = get_option('taxonomy_' . $term_id);
echo $productCatMetaTitle = $metaArray['wh_meta_title'];
echo $productCatMetaDesc = $metaArray['wh_meta_desc'];
这段代码应该放在你的活动子主题(或主题)或任何插件文件中的function.php文件中。
所有代码都经过测试,完全可用。
参考资料
官方文档:
从WordPress 4.4开始,您应该考虑等待Woocommerce的升级或手动开始填充新的wp_termmeta表。
WP版本4.4及以上存在代码问题。
当您在类别列表管理页面更改任何内容(例如URL slug或名称)时,自定义字段值会更改为空字符串。
我添加了编辑保存功能,但不会在类别编辑管理页面上执行:
// Save extra taxonomy fields callback function - create.
function wh_save_taxonomy_custom_meta_create($term_id) {
$wh_meta_title = filter_input(INPUT_POST, 'wh_meta_title');
$wh_meta_desc = filter_input(INPUT_POST, 'wh_meta_desc');
update_term_meta($term_id, 'wh_meta_title', $wh_meta_title);
update_term_meta($term_id, 'wh_meta_desc', $wh_meta_desc);
}
// Save extra taxonomy fields callback function - edit.
function wh_save_taxonomy_custom_meta($term_id) {
$screen = get_current_screen();
if ( $screen->id != 'edit-product_cat' )
return; // exit if incorrect screen id
$wh_meta_title = filter_input(INPUT_POST, 'wh_meta_title');
$wh_meta_desc = filter_input(INPUT_POST, 'wh_meta_desc');
update_term_meta($term_id, 'wh_meta_title', $wh_meta_title);
update_term_meta($term_id, 'wh_meta_desc', $wh_meta_desc);
}
add_action('edited_product_cat', 'wh_save_taxonomy_custom_meta', 10, 1);
add_action('create_product_cat', 'wh_save_taxonomy_custom_meta_create', 10, 1);