亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 WooCommerce 中顯示鏈接到隨機產品的自定義圖像

在 WooCommerce 中顯示鏈接到隨機產品的自定義圖像

PHP
幕布斯6054654 2023-07-08 21:47:42
在 WooCommerce 中,我希望我的客戶選擇一個圖像,當他們單擊該圖像時,該圖像會將他們帶到隨機產品。獲取隨機產品 id(數組):$random_product_array = get_posts( array( 'posts_per_page' => 1, 'post_type' => 'product', 'orderby' => 'rand', 'fields' => 'ids' ) ); $random_product_id    = reset($random_product_array); // Get the random product ID顯示隨機產品的鏈接按鈕:echo '<a href= "www.mylink.com/“> <img alt= “mylink” src=https://www.mylink.com/images/promo pic.png get_permalink($random_product_id) . '" class="img  alt">' width=150” height=“70”</a>';
查看完整描述

1 回答

?
尚方寶劍之說

TA貢獻1788條經驗 獲得超4個贊

您基本上就在那里,只是錯過了正確位置的產品永久鏈接。


這里使用WP_Query:


// Get a random product (array with one value)

$random_product_id_array = get_posts( array( 

    'posts_per_page' => 1, 

    'post_type' => 'product', 

    'orderby' => 'rand', 

    'fields' => 'ids' 

) );


// Get the first value from the array (the random product ID)

$random_product_id = reset($random_product_array);


// Output

echo '<a href="'.get_permalink($random_product_id).'"><img alt="mylink" src="https://www.mylink.com/images/promo-pic.png" class="img" width="150" height="70"></a>';

這次成功了。


或者您也可以使用WC_Product_query類似的替代:


// Get a random product (array with one value)

$random_product_id_array = wc_get_products( array(

    'limit' => 1,

    'orderby' => 'rand',

    'return' => 'ids'

) );


// Get the first value from the array (the random product ID)

$random_product_id = reset($random_product_array);


// Output

echo '<a href="'.get_permalink($random_product_id).'"><img alt="mylink" src="https://www.mylink.com/images/promo-pic.png" class="img" width="150" height="70"></a>';

也以同樣的方式工作。


另外:您可以將該代碼嵌入到短代碼中,例如(使用WC_Product_query):


add_shortcode('random_img_link', 'display_random_img_link');

function display_random_img_link() {

    // Get a random product (array with one value)

    $query = wc_get_products( array(

        'limit' => 1,

        'orderby' => 'rand',

        'return' => 'ids'

    ) );

    

    // Here define your image link

    $image_src = 'https://www.mylink.com/images/promo-pic.png';


ob_start(); // Start buffering


echo '<a href="'.get_permalink(reset($query)).'"><img alt="mylink" src="'.$image_src.'" class="img" width="150" height="70"></a>';


return ob_get_clean(); // return  buffered content

}

或者(使用WP_Query):


add_shortcode('random_img_link', 'display_random_img_link');

function display_random_img_link() {

    // Get a random product (array with one value)

    $query = get_posts( array( 

        'posts_per_page' => 1, 

        'post_type' => 'product', 

        'orderby' => 'rand', 

        'fields' => 'ids' 

    ) );

    

    // Here define your image link

    $image_src = 'https://www.mylink.com/images/promo-pic.png';


ob_start(); // Start buffering


echo '<a href="'.get_permalink(reset($query)).'"><img alt="mylink" src="'.$image_src.'" class="img" width="150" height="70"></a>';


return ob_get_clean(); // return  buffered content

}

代碼位于活動子主題(或活動主題)的functions.php 文件中。經過測試并有效。


用法: [random_img_link]


查看完整回答
反對 回復 2023-07-08
  • 1 回答
  • 0 關注
  • 198 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號