Creating a different template file for certain Pro

2019-05-31 08:05发布

I am trying to make different template file depending on the category a certain Woocommerce product is listed in.

I have Googled endlessly to find a solution. I have tried creating a template with the name "taxonomy-product_cat-[slugnamehere].php" and it didn't work. I have tried making making a template file, then, using if/else statements setting the template file in single_product.php. It isn't working either.

The category for my product is called 'box'. Below is the code from single_product; it isn't seeming to work. Any advice would be appreciated:

} else if ( is_product_category() ) {
            if ( is_product_category( 'box' ) ) {
                woocommerce_get_template_part( 'content', 'single-product-sample-box-4');
            }               
        }
        else {
            woocommerce_get_template_part( 'content', 'single-product');
        }

I want, if the category is 'box' is selected, for the content-single-product-sample-box-4 template to be displayed - instead only the content-single-product template is being displayed.

1条回答
爷的心禁止访问
2楼-- · 2019-05-31 08:38

I found an answer! For anyone else struggling with this I will post my solution... I was only trying to change the template for ONE product (it was a sample box and therefore different from all other items).

Instead of this:

} else if ( is_product_category() ) {
        if ( is_product_category( 'box' ) ) {
            woocommerce_get_template_part( 'content', 'single-product-sample-box-4');
        }               
    }
    else {
        woocommerce_get_template_part( 'content', 'single-product');
    }

I did this:

} else if ( get_the_ID() == [PAGE ID GOES HERE] ) {
            woocommerce_get_template_part( 'content', 'single-product-sample-box-4');

        }
        else {
            woocommerce_get_template_part( 'content', 'single-product');
        }
查看更多
登录 后发表回答