我正在尝试获取具有特定类别的所有产品。如果我从代码中删除产品类别代码,它将显示数据,否则不显示任何内容。我确定使用的产品类别正确。
$args = array(
'post_type' => 'product_variation',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'men'
),
),
);
$post_query = new WP_Query($args);
if($post_query->have_posts() ) {
while($post_query->have_posts() ) {
$post_query->the_post();
$tre = get_post_meta(get_the_ID(), '_xoo-wl-users', true );
$data = json_decode($tre);
foreach($data as $key => $value)
{
echo $key;
echo "<br>";
}
}
}
答案 0 :(得分:0)
在$args
中使用以下代码,因此它将仅显示men
类别的产品
<?php $args = array( 'post_type' => 'product', 'posts_per_page' => -1, 'product_cat' => 'men', 'orderby' => 'date' ); ?>
答案 1 :(得分:0)
尝试一下:
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'men', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<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>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>