Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
807 views
in Technique[技术] by (71.8m points)

wordpress - Woocommerce single product - template by categories

Already posted question but not working for me, so I'm asking a little help guys... ;)

There is my single-product.php (woocommerce file line:28)

 <?php if ( is_product_category( 'customcateg' ) ) 
    {
       woocommerce_get_template_part( 'content', 'single-product-customcateg' ); }
else {      
       woocommerce_get_template_part( 'content', 'single-product' ); } 
  ?>

I have in my theme/woocommerce 3 files:

  • single-product.php
  • content-single-product.php
  • content-single-product-customcateg.php

The files contains different php codes (the customcateg has to be protected from visitors, and some roles; this part is already done...)

When i try to access a product who is in "customcateg", it shows me the normal "single-product" template....

I tried to put :

 <?php if ( is_product_category( 'customcateg' ) ) 
    {
       woocommerce_get_template_part( 'content', 'single-product-customcateg' ); }
else {      
       woocommerce_get_template_part( 'content', 'single-product-customcateg' ); } 
  ?>

It simply show the single-product, but it has to show the customcateg one, but it does not. It only does when I pust the "single-product-customcateg.php" content in the "single-product.php" ...

What's wrong guys ?

Please help me :)

Thanks a lot by advance,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's looking for a file titled content-single-product-customcateg.php

That file doesn't exist so it's falling back to the default.

if ( has_term( 'customcateg', 'product_cat' ) ) {
    woocommerce_get_template_part( 'content', 'single-product-customcateg' );
} else {
    woocommerce_get_template_part( 'content', 'single-product' );
}

Can you add the contents of your content-single-product-customcateg.php by the way?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...