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

Display a custom image linked to a random product in WooCommerce

在 WooCommerce 中,我希望我的客户 select 一张图片,当他们点击它时会将他们带到随机产品。

获取随机产品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>';

你大部分都在那里,你只是错过了正确位置的产品永久链接。

此处使用 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]