加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

php – 将Woocommerce父类添加到WP’body’类

发布时间:2020-05-31 01:19:25 所属栏目:PHP 来源:互联网
导读:我正在尝试将Woocommerce中的产品父类别添加为wordpress的body标签. 每次进入子类别时,父类别不再在body类中. 可以编辑下面的内容来查找父类别并在body标签中添加吗? 也许像“product_parent_cat”这样的术语?试过这个并搜索他们的API但没有成功.. function

我正在尝试将Woocommerce中的产品父类别添加为wordpress的body标签.

每次进入子类别时,父类别不再在body类中.

可以编辑下面的内容来查找父类别并在body标签中添加吗?

也许像“product_parent_cat”这样的术语?试过这个并搜索他们的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' );
您可以尝试此修改(未经测试):
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' );

将父产品类别slugs添加到body类.

这里我们使用get_term()函数返回的term对象的parent属性.

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读