为了账号安全,请及时绑定邮箱和手机立即绑定

在 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 关注
  • 99 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信