Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在WooCommerce中添加与产品类别相关的主体类_Php_Wordpress_Woocommerce_Wordpress Theming_Categories - Fatal编程技术网

Php 在WooCommerce中添加与产品类别相关的主体类

Php 在WooCommerce中添加与产品类别相关的主体类,php,wordpress,woocommerce,wordpress-theming,categories,Php,Wordpress,Woocommerce,Wordpress Theming,Categories,如何添加与产品类别slug相关的body类 谢谢大家! 在Wordpress支持网站上有一个很好的指南,可以帮助您做到这一点: 为了满足您的需要,我已经修改了邮件中的代码: add_filter('body_class','add_category_to_single'); function add_category_to_single($classes, $class) { if (is_single() ) { global $post; foreac

如何添加与产品类别slug相关的body类


谢谢大家!

在Wordpress支持网站上有一个很好的指南,可以帮助您做到这一点:

为了满足您的需要,我已经修改了邮件中的代码:

add_filter('body_class','add_category_to_single');
function add_category_to_single($classes, $class) {
    if (is_single() ) {
        global $post;
        foreach((get_the_category($post->ID)) as $category) {
            echo $category->cat_name . ' ';
            // add category slug to the $classes array
            $classes[] = $category->slug;
        }
    }
    // return the $classes array
    return $classes;
}

将其添加到functions.php中

add_filter( 'body_class', 'wc_cat_names' );
function wc_cat_names( $classes ) {
    if(is_product()){
    global $post;
    $terms = get_the_terms( $post->ID, 'product_cat' );
        foreach ($terms as $term) {
            $classes[] = $term->slug;
        }
    }
    return $classes;
}

这篇文章只适用于woocommerce产品页面,是的,我在我的测试网站上测试了这篇文章。

Hi,请在发布前阅读本文:并阅读Stackover提供的所有文章,以提出好问题:老实说,不,不是在回答时。我在以前的一个项目中使用了它的一个修改版本,这就是我记得这篇文章的原因。但据我所知,它应该仍然是一个有效的钩子。这给了我一个php错误,并将body_类添加到另一个div。错误:
警告:缺少add_category_to_single的参数2(
Yes al right for the hook,是一个经典的钩子,没问题……但是woocommerce类别与wordpress类别有点不同……别担心,只是问问:)这是你的主意……但正如我所说的,未经测试。太多的
声誉
机会主义,对不起。