如何从wp_termea添加Body_CLASS

时间:2016-03-13 作者:zaharbaz

我需要为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类别时有“输入”。http://i.stack.imgur.com/Tc1dC.png数据存储在wp\\U termmeta中:http://i.stack.imgur.com/SLGSk.png

好的

但我现在不知道如何在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;
}

1 个回复
最合适的回答,由SO网友:Sumit 整理而成

首先使用以下内容检查它是产品类别存档页面is_taxget queried object_id 获取当前term IDget_term_meta 获取类名

add_filter( \'body_class\', \'lalka_custom_taxonomy_in_body_class\' );
function lalka_custom_taxonomy_in_body_class( $classes ){
    if (is_tax(\'product_cat\')) {

        $custom_class = !empty( get_queried_object_id() ) ? get_term_meta(get_queried_object_id(), \'custom-class\', true) : FALSE;
        if ( !empty($custom_class) ) {
            $classes[] = \'custom-tax-\' . $custom_class;
        }

    }
      return $classes;
}

相关推荐

Functions.php中的上次更新帖子快捷代码

我正在搜索在函数中创建一个短代码。php子主题,用缩略图显示上次更新的帖子。我有以下代码可用于页面模板: <ol class="list-numbered"> <?php // Show recently modified posts $recently_updated_posts = new WP_Query( array( \'post_type\' => \'post\