php Woocommerce获得多个类别的产品

yvt65v4c  于 5个月前  发布在  PHP
关注(0)|答案(4)|浏览(51)

我正在为我的woocommerce网站上的每个类别设置随机缩略图。默认情况下,会分配一个占位符,但您可以通过在 Jmeter 板中上传/分配图像来覆盖它。
我想做的是,例如,“男装”是我的父类别和“牛仔裤”是我的子类别。所以我想显示一个项目的随机图像是在“男装>>牛仔裤”类别.我希望这是有意义的。
我现在想到的是这个(在我的wc_template_functions. php文件中)

args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => $category->name, 'orderby' => 'rand' );
$loop = new WP_Query( $args );

while ( $loop->have_posts() ) { $loop->the_post();}{ 

$image = print_r(get_the_post_thumbnail($loop->post->ID, 'shop_catalog'));}
    $image = $image[0];     

$image =  apply_filters( 'woocommerce_placeholder_img_src',$image[0] );

字符串
也就是生成图像,但我遇到的问题是,假设我在父类别“Mens”和“Womens”下有“Jeans”,显示的图片完全忽略了它是在“Mens”还是“Womens”类别中。所以我在“Men >> Jeans”中得到了女性牛仔裤的图像。
我需要做的是能够指定两个类别,“牛仔裤”和“男装”,但我不确定如何做到这一点.我已经被告知,因为产品不是职位我不能指定产品类别名称,我必须使用分类?
有人能帮我指个路吗?
干杯克里斯

jhkqcmku

jhkqcmku1#

这就是你要做的:

$query_args = array( 
    'post_status' => 'publish', 
    'post_type' => 'product',
    'tax_query' => array(
        array(
                'taxonomy' => 'product_cat',
                'field' => 'id',
                'terms' => array( 27, 25 ),
                'operator' => 'IN'
        )
    )
);

$loop = new WP_Query($query_args);

while ($loop->have_posts() )
{
    //Setup post data
    $loop->the_post();
    
    echo get_the_title();
    
}

字符串
这将获得ID为25和27的两个类别的产品。如果您更愿意通过slugs指定,您也可以使用'field' => 'slug''+,但我认为在这种情况下使用ID会更快地查询。

wydwbb8l

wydwbb8l2#

<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">

   <?php woocommerce_show_product_sale_flash( $post, $product ); ?>

                        <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
    <h3><?php the_title(); ?></h3>

<?php endwhile; ?>
    <?php wp_reset_query(); ?>

字符串

tf7tbtn2

tf7tbtn23#

<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">

<?php woocommerce_show_product_sale_flash( $post, $product ); ?>

<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>

<h3><?php the_title(); ?></h3>

字符串

8fq7wneg

8fq7wneg4#

您可以使用以下代码(根据需要修改代码)执行一个简单的查询,以获取属于多个类别的所有产品..

$query_args = array( 
    'post_status' => 'publish', 
    'post_type' => 'product',
    'tax_query' => array(
        array(
            'taxonomy' => 'product_cat',
            'field' => 'id', // or use 'slug'
            'terms' => array( 76494, 78671 ), // 'womens-underwear', 'lingerie'
            'operator' => 'IN'
        )
    )
);

$query = new WP_Query( $query_args );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        $post_id = get_the_id(); // same as `$product_id` below
        $post_title = get_the_title(); // same as `$product_name` below
        // global $product; // -- WooCommerce Product Object --
        // $product_id = $product->get_id();
        // $product_name = $product->get_name();
        // $product_sku = $product->get_sku();
        // $product_price = $product->get_price();
        // $product_stock_quantity = $product->get_stock_quantity();
        // $product_stock_status = $product->get_stock_status();
        echo '<pre>'; echo $post_title.' ('.$post_id.')'; echo '</pre>';
    }
}

字符串
本例中的结果(回声)

Classic Print Thong – Olive Leopard (3765931)
Classic Print Thong – Olive Leopard (3765928)
Women’s Eco Scene Brief Bikini Bottom – Acid Purple (3765924)
Women’s Eco Scene Brief Bikini Bottom – Baby Pink (3765920)
Women’s Logo Band Jersey Thong – Parade Red (3765916)

相关问题