第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

在 WooCommerce 中顯示鏈接到隨機產(chǎn)品的自定義圖像

在 WooCommerce 中顯示鏈接到隨機產(chǎn)品的自定義圖像

PHP
幕布斯6054654 2023-07-08 21:47:42
在 WooCommerce 中,我希望我的客戶選擇一個圖像,當(dāng)他們單擊該圖像時,該圖像會將他們帶到隨機產(chǎn)品。獲取隨機產(chǎn)品 id(數(shù)組):$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顯示隨機產(chǎn)品的鏈接按鈕: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貢獻(xiàn)1788條經(jīng)驗 獲得超4個贊

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


這里使用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 文件中。經(jīng)過測試并有效。


用法: [random_img_link]


查看完整回答
反對 回復(fù) 2023-07-08
  • 1 回答
  • 0 關(guān)注
  • 181 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號