douhezi2285 2017-02-14 18:52
浏览 45

woocommerce - 查询具有相同标签的产品

I found a few articles with examples on how to achieve this, but they don't output any data for me. Here's an example:

function woo_products_by_tags_shortcode( $atts, $content = null ) {

    // Get attribuets
    extract(shortcode_atts(array(
        "tags" => ''
    ), $atts));

    ob_start();

    // Define Query Arguments
    $args = array( 
                'post_type'      => 'product', 
                'posts_per_page' => 5, 
                'product_tag'    => $tags 
                );

    // Create the new query
    $loop = new WP_Query( $args );

    // Get products number
    $product_count = $loop->post_count;

    // If results
    if( $product_count > 0 ) :

        echo '<ul class="products">';

            // Start the loop
            while ( $loop->have_posts() ) : $loop->the_post(); global $product;

                global $post;

                echo "<p>" . $thePostID = $post->post_title. " </p>";

                if (has_post_thumbnail( $loop->post->ID )) 
                    echo  get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); 
                else 
                    echo '<img src="'.$woocommerce->plugin_url().'/assets/images/placeholder.png" alt="" width="'.$woocommerce->get_image_size('shop_catalog_image_width').'px" height="'.$woocommerce->get_image_size('shop_catalog_image_height').'px" />';

            endwhile;

        echo '</ul><!--/.products-->';

    else :

        _e('No product matching your criteria.');

    endif; // endif $product_count > 0

    echo ob_get_clean();

}

This code is a shortcode, and I don't want to have to write one out and input the tag name manually. I want to use this function to hook into woocommerce_after_single_product_summary. Basically I'm trying to achieve this.

The first post is the current post. After that it's up to 3 more posts that will be visible. The query needs to look at the current product's tag, find more in it, and output ordered by the product category.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度