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

时间:2017-02-14 18:52:24

标签: php wordpress woocommerce

我发现了一些关于如何实现这一目的的文章,但它们并没有为我输出任何数据。这是一个例子:

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();

}

此代码是一个短代码,我不想写一个并手动输入标记名称。我想用这个函数挂钩woocommerce_after_single_product_summary。基本上我正在努力实现this

第一篇文章是当前的帖子。之后,最多可以看到3个帖子。查询需要查看当前产品的标签,在其中查找更多内容以及按产品类别排序的输出。

0 个答案:

没有答案
相关问题