我將以下代碼用于我的商店單頁側邊欄,我通過變體名稱(型別 2)設定影像。
function image_by_variation() {global $product;if ( ! $product->get_attribute( 'type-2' )) return;echo '<div ><img src="https://ksa.revent.store/wp-content/uploads/2022/09/repaired.png" height="200" width="500"></div>';}
螢屏截圖 - https://prnt.sc/o__rry_OT4g5 現在我的 2 型變體有兩個專案,
- 修復
- 從未修復
我想通過變化來改變影像橫幅。因為我只會為一種產品選擇一種變體。有時(修復)有時(從未修復)
Like--> if select repaired (image1), or select never-repaired (image2)
uj5u.com熱心網友回復:
你可以這樣做:
function image_by_variation() {
global $product;
// Get the type attribute and store in variable.
$type = $product->get_attribute( 'type-2' );
// Check if type value is empty?
if ( empty( $type ) ) {
return;
}
// Compare with repaired.
if ( 'repaired' === $type ) {
echo '<div ><img src="https://ksa.revent.store/wp-content/uploads/2022/09/repaired.png" height="200" width="500"></div>';
return;
}
// Compare with never repaired.
if ( 'never-repaired' === $type ) {
echo '<div ><img src="https://ksa.revent.store/wp-content/uploads/2022/09/never-repaired.png" height="200" width="500"></div>';
return;
}
}
注意:如果$product->get_attribute( 'type-2' )回傳一個陣列,那么您必須更改條件以檢查repaired或是否never-repaired存在于該陣列中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/505829.html
標籤:php WordPress 图片 woocommerce 钩woocommerce
上一篇:顯示意外行為的錨點
下一篇:影像按鈕在布局上放大/裁剪?
