Wordpress WooCommerce自定义主题(按类别)

Wordpress WooCommerce自定义主题(按类别),wordpress,woocommerce,Wordpress,Woocommerce,我正在尝试为WooCommerce中的产品类别创建特定的Woo模板 我试着按照这里的说明去做,但它们对我不起作用 我也发现了这个,但没法让它工作 在我的主题中,我有一个woocommerce文件夹,其中包含我要覆盖的特定模板文件 theme/woocommerce/single-product.php theme/woocommerce/content-single-product.php theme/woocommerce/content-single-product-custom-1.php

我正在尝试为WooCommerce中的产品类别创建特定的Woo模板

我试着按照这里的说明去做,但它们对我不起作用

我也发现了这个,但没法让它工作

在我的主题中,我有一个woocommerce文件夹,其中包含我要覆盖的特定模板文件

theme/woocommerce/single-product.php
theme/woocommerce/content-single-product.php
theme/woocommerce/content-single-product-custom-1.php
这里是single-product.php的一部分

<?php if (is_product_category( 'custom-1')) {
    wc_get_template_part( 'content', 'single-product-custom-1' );
    }
    elseif (is_product_category( 'promise') {
    wc_get_template_part( 'content', 'single-product-custom-2' );
    }
    else{
    wc_get_template_part( 'content', 'single-product' );
    } 
?>

问题在于其行为与或Wordpress函数相同,这意味着它会检查是否正在显示类别归档页(在本例中为自定义分类归档页)。如果您在单个帖子页面中,您可以使用以下功能(检查当前帖子是否包含任何给定的术语)或(检索附在帖子上的分类术语),在Woocomeerce中,分类法是'product\u cat'



将其添加到functions.php文件中,以将product_cat_CATEGORYNAME术语包含在products body类中

function woo_custom_taxonomy_in_body_class( $classes ){
if( is_singular( 'product' ) )
{
    $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' );

现在可以引用product_cat术语了

我尝试了你的代码,但仍然不起作用。。。是否可能我没有正确添加分类术语?我试着在woo中将“custom-1”添加为一个类别,并尝试将其添加为标签,但仍然没有任何结果。有可能通过一个帖子id号来定位我的自定义主题吗?所以,我只是想更好地掌握一切。。。因此,wooCommerce添加了分类术语“product_cat”,而另一个术语是基于分类术语的如果你在循环之外,或者
打印(获取术语($product->ID,'product\u cat')在循环内(全部在'single product.php'模板中),查看'product_cat'分类法的术语是否正确附加到产品。如果你没有得到任何输出,那么你就做错了。我一定是做错了什么,因为我没有得到任何输出。下面是我在single-product.php中的内容,顺便说一下,我更改了第二个类别名称。还有什么我可以粘贴的东西可以帮助你看到问题吗?我真的很感谢你的帮助。非常感谢。您删除了整个循环,请立即尝试。若你们仍然并没有得到任何输出,那个么模板甚至并没有被加载
function woo_custom_taxonomy_in_body_class( $classes ){
if( is_singular( 'product' ) )
{
    $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' );