2015-10-06 71 views
0

我正在與woocommerce集成的wordpress網站上工作。作爲標題的Woocommerce屬性

現在我想重寫項目的標題與同一項目的自定義屬性的值(在產品檔案頁和單產品頁)。

我搞亂了functions.php,但沒有任何工作適合我。

任何woocommerce/.php專家在這裏?提前致謝!

回答

2

您需要使用the_title過濾器以及in_the_loop,is_shopis_product有條件的如下所示。添加以下代碼您的主題的功能.php文件

add_filter('the_title', 'custom_the_title'); 

function custom_the_title($title, $id) { 

    global $product; 

    /* 
    * We use is_shop and is_product to check if the page is product archive 
    * or single product page. in_the_loop conditional is used to check if the loop 
    * is currently active, otherwise it will over write the page title as well 
    */ 

    if((is_shop() || is_product()) && in_the_loop()) { 

     $attr = $product->get_attributes(); 

     // $attr contains the attribute array, apply the logic you need 
     // and set $title 

     return $title; 

    } 

    return $title; 
} 
+0

或者'woocommerce_product_title'是一個只能在產品上運行的過濾器。 – helgatheviking

+0

@helgatheviking,我同意。但根據我的發現,它不適用於產品歸檔和單個產品頁面。它在購物車和結賬頁面上工作。 –

+0

是的,它確實在我的網頁上工作,你能給我一個建議如何操縱過濾器@helgatheviking? –