Php 将Woocommerce父类别添加到WP';车身';班
我正在尝试将Woocommerce中的产品父类别作为一个类添加到wordpress的“Php 将Woocommerce父类别添加到WP';车身';班,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我正在尝试将Woocommerce中的产品父类别作为一个类添加到wordpress的“body标记中 每次我进入子类别时,父类别不再在body类中 是否可以编辑以下内容以查找父类别并在body标记中添加 也许是像“产品、父母、猫”这样的术语?尝试了这个并搜索了他们的API,但没有成功 function woo_custom_taxonomy_in_body_class( $classes ){ $custom_terms = get_the_terms(0, 'product_cat')
body
标记中
每次我进入子类别时,父类别不再在body
类中
是否可以编辑以下内容以查找父类别并在body标记中添加
也许是像“产品、父母、猫”这样的术语?尝试了这个并搜索了他们的API,但没有成功
function woo_custom_taxonomy_in_body_class( $classes ){
$custom_terms = get_the_terms(0, 'product_cat');
if ($custom_terms) {
foreach ($custom_terms as $custom_term) {
$classes[] = 'product_cat_' . $custom_term->slug;
}
}
return $classes;
}
add_filter( 'body_class', 'woo_custom_taxonomy_in_body_class' );
您可以尝试此修改(未测试): 将父产品类别Slug添加到body类
这里我们使用
get_term()
函数返回的term对象的parent
属性。2017年新闻。仍在使用Woocommerce 3.0+和Wordpress 4.8
function woo_custom_taxonomy_in_body_class( $classes ){
$custom_terms = get_the_terms(0, 'product_cat');
if ($custom_terms) {
foreach ($custom_terms as $custom_term) {
// Check if the parent category exists:
if( $custom_term->parent > 0 ) {
// Get the parent product category:
$parent = get_term( $custom_term->parent, 'product_cat' );
// Append the parent class:
if ( ! is_wp_error( $parent ) )
$classes[] = 'product_parent_cat_' . $parent->slug;
}
$classes[] = 'product_cat_' . $custom_term->slug;
}
}
return $classes;
}
add_filter( 'body_class', 'woo_custom_taxonomy_in_body_class' );