![](/img/trans.png)
[英]Display custom field blow product title Woocommerce product single pages
[英]Display a custom field above product price in Woocommerce single product pages
在Woocommerce中,我已經以正確的方式在支持的編輯產品頁面中設置了一個自定義字段...現在我嘗試在產品價格之前將產品顯示為自定義字段值到單個產品頁面中。
但出於某種原因,使用下面的代碼我可以顯示它(並且產品價格也沒有):
function bd_rrp_price_html( $price, $product ) {
echo get_post_meta( $post->ID, '_text_field', true );
}
add_filter( 'woocommerce_get_price_html', 'bd_rrp_price_html', 100, 2 );
任何幫助表示贊賞
這是我想要在視覺上顯示的內容:
更新(2個替代方案)
請嘗試以下重新訪問的代碼:
add_filter( 'woocommerce_get_price_html', 'custom_single_price_html', 100, 2 );
function custom_single_price_html( $price, $product ) {
$custom_field = get_post_meta( $product->get_id(), '_text_field', true );
if ( is_product() && ! empty($custom_field) )
$price = '<span class="custom-textfield">' . $custom_field . '</span><br>' . $price;
return $price;
}
代碼位於活動子主題(或活動主題)的function.php文件中。 經過測試和工作。
或者您也可以使用以下內容:
add_filter( 'woocommerce_single_product_summary', 'single_product_text_field', 8 );
function single_product_text_field() {
global $product;
$custom_field = get_post_meta( $product->get_id(), '_text_field', true );
if ( ! empty($custom_field) )
echo '<p class="custom-textfield">' . $custom_field . '</p>';
}
代碼位於活動子主題(或活動主題)的function.php文件中。 經過測試和工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.