【问题标题】:Display a custom image linked to a random product in WooCommerce在 WooCommerce 中显示链接到随机产品的自定义图像
【发布时间】:2020-07-03 20:35:22
【问题描述】:

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

【问题讨论】:

    标签: php wordpress random woocommerce permalinks


    【解决方案1】:

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

    这里使用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]

    【讨论】:

    • 它似乎没有加载新产品,我做错了什么
    • @mosley92 是的,很奇怪……我已经更新了我的答案代码……这次它可以工作了。
    • 它有效。谢谢。我认为这可能是我将其应用于原始代码的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 2019-06-25
    • 2019-02-27
    相关资源
    最近更新 更多