2014-08-28 118 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