2014-08-28 49 views
3

我需要为我的woocommerce网站中的每个产品制作单独的模板。创建单个产品模板

是否有做类似像single-product-9455.php

我相信我可以使用类别这样

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

做模板的东西的方法,但因为每个产品需要定制,这将导致相当多的不必要的类别模板。

有没有一种方法来定位产品ID?

正文css类是不够的。我需要为每个模板。

回答

2

你可以,但你不想这样做。如果网站增长,它可能变得非常混乱。一个明智的方向将是和使用conditionals

否则,如果兔子洞听起来不错,你可能要为每一个产品一个新的类别挂钩条件...

1)在你的主题目录中创建一个woocommerce文件夹,如果它不”不存在。

2)在这个目录下名为“woocommerce”重复内容单product.php文件并命名新内容单一产品TEMPLATENAME.php

而且复制woocommerce模板从woocommerce文件单product.php到您的目录名为woocommerce并找到如下因素线:

3)发现在单亲以下行您刚刚复制过的duct.php

woocommerce_get_template_part('content', 'single-product'); 

更改为:

if (is_product_category('1CATEGORY') // Where this is the category name 
{ 
    // Category name 1 
    woocommerce_get_template_part('content', 'single-product-TEMPLATENAME'); 
} 
elseif (is_product_category('2CATEGORYNAME') 
{ 
    // category name 2 
    woocommerce_get_template_part('content', 'single-product-TEMPALTENAME2'); 
} 
else 
{ 
    // You will allows want to default to the single product template. 
    woocommerce_get_template_part('content', 'single-product'); 
} //endif 

如果有办法从你不会希望一个产品ID挂钩。这需要产品提前到场,这从开发/商业的角度来看是没有意义的。

欢迎修订。

+1

在这种情况下使用开关会更好吗?默认等价于你的else语句吗?没有功能差异只是更容易阅读和更新。 – Dez 2014-08-28 19:17:01

+1

@Dez为了可读性,可能。我说可能是因为我不确定封装在这个特定情况下有多重要。 IF/Switch语句都以不同的方式处理变量作用域。在Java中也是如此。 – 2014-08-28 19:34:07