我需要为woocommerce类别的主体分配一个类。
我所做的:
// add the fields when the term is created
add_action( \'product_cat_add_form_fields\', \'add_product_cat_class_field\', 10, 2 );
function add_product_cat_class_field($taxonomy) {
global $feature_groups;
?><div class="form-field term-group">
<label for="feature-group"><?php _e(\'Custom CSS Class\', \'my_plugin\'); ?></label>
<input type="text" class="postform" id="custom-class" name="custom-class" value="">
</select>
</div><?php
}
// save the term meta
add_action( \'created_product_cat\', \'save_product_cat_class_meta\', 10, 2 );
function save_product_cat_class_meta( $term_id, $tt_id ){
if( isset( $_POST[\'custom-class\'] ) && \'\' !== $_POST[\'custom-class\'] ){
$custom_class = sanitize_title( $_POST[\'custom-class\'] );
add_term_meta( $term_id, \'custom-class\', $custom_class, true );
}
}
// add the fields when the term is being edited
add_action( \'product_cat_edit_form_fields\', \'edit_product_cat_class_field\', 10, 2 );
function edit_product_cat_class_field( $term, $taxonomy ){
global $feature_groups;
// get current group
$custom_class = get_term_meta( $term->term_id, \'custom-class\', true );
?><tr class="form-field term-group-wrap">
<th scope="row"><label for="feature-group"><?php _e( \'Feature Group\', \'my_plugin\' ); ?></label></th>
<td><input type="text" class="postform" id="custom-class" name="custom-class" value="<?php echo esc_attr( $custom_class ); ?>"></td>
</tr><?php
}
add_action( \'edited_product_cat\', \'update_product_cat_class_meta\', 10, 2 );
function update_product_cat_class_meta( $term_id, $tt_id ){
if( isset( $_POST[\'custom-class\'] ) && \'\' !== $_POST[\'custom-class\'] ){
$custom_class = sanitize_title( $_POST[\'custom-class\'] );
update_term_meta( $term_id, \'custom-class\', $custom_class );
}
}
I\'ts工作。我在创建和编辑woocommerce类别时有“输入”。
数据存储在wp\\U termmeta中:
好的
但我现在不知道如何在body\\u类中获得它(meta\\u值)
我试过这个:
add_filter( \'body_class\', \'lalka_custom_taxonomy_in_body_class\' );
function lalka_custom_taxonomy_in_body_class( $classes ){
global $feature_groups;
$custom_class = get_the_terms(0, \'$product_cat\');
// or
//$custom_class = get_term_meta($term_id, \'custom-class\', true );
if (!empty($custom_class) ) {
$classes[] = \'custom-tax-\' . $custom_class;
}
return $classes;
}