猿问

在 WooCommerce 中显示链接到随机产品的自定义图像

在 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>';



幕布斯6054654
浏览 143回答 1
1回答

尚方宝剑之说

您基本上就在那里,只是错过了正确位置的产品永久链接。这里使用WP_Query:// Get a random product (array with one value)$random_product_id_array = get_posts( array(&nbsp;&nbsp; &nbsp; 'posts_per_page' => 1,&nbsp;&nbsp; &nbsp; 'post_type' => 'product',&nbsp;&nbsp; &nbsp; 'orderby' => 'rand',&nbsp;&nbsp; &nbsp; 'fields' => 'ids'&nbsp;) );// Get the first value from the array (the random product ID)$random_product_id = reset($random_product_array);// Outputecho '<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(&nbsp; &nbsp; 'limit' => 1,&nbsp; &nbsp; 'orderby' => 'rand',&nbsp; &nbsp; 'return' => 'ids') );// Get the first value from the array (the random product ID)$random_product_id = reset($random_product_array);// Outputecho '<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() {&nbsp; &nbsp; // Get a random product (array with one value)&nbsp; &nbsp; $query = wc_get_products( array(&nbsp; &nbsp; &nbsp; &nbsp; 'limit' => 1,&nbsp; &nbsp; &nbsp; &nbsp; 'orderby' => 'rand',&nbsp; &nbsp; &nbsp; &nbsp; 'return' => 'ids'&nbsp; &nbsp; ) );&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // Here define your image link&nbsp; &nbsp; $image_src = 'https://www.mylink.com/images/promo-pic.png';ob_start(); // Start bufferingecho '<a href="'.get_permalink(reset($query)).'"><img alt="mylink" src="'.$image_src.'" class="img" width="150" height="70"></a>';return ob_get_clean(); // return&nbsp; buffered content}或者(使用WP_Query):add_shortcode('random_img_link', 'display_random_img_link');function display_random_img_link() {&nbsp; &nbsp; // Get a random product (array with one value)&nbsp; &nbsp; $query = get_posts( array(&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 'posts_per_page' => 1,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 'post_type' => 'product',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 'orderby' => 'rand',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 'fields' => 'ids'&nbsp;&nbsp; &nbsp; ) );&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // Here define your image link&nbsp; &nbsp; $image_src = 'https://www.mylink.com/images/promo-pic.png';ob_start(); // Start bufferingecho '<a href="'.get_permalink(reset($query)).'"><img alt="mylink" src="'.$image_src.'" class="img" width="150" height="70"></a>';return ob_get_clean(); // return&nbsp; buffered content}代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。用法: [random_img_link]
随时随地看视频慕课网APP
我要回答