admin管理员组文章数量:1278826
I don't know why i can't take the products by category name. I get no product found. The code look like this
<ul class="glide__slides">
<?php
$args = array(
'post_type' => 'product',
'category_name' => 'trofee',
'posts_per_page' => 20,
'meta_query' => array(
'relation' => 'OR',
array( // Simple products type
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array( // Variable products type
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$id = $product->get_id();
echo '<li class="glide__slide">';
echo '<br /><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().' <br> '.get_the_title(). '<br>' . '<div class="bf-sale"><span>' . $product->get_regular_price() .' Lei </span>' . '<span>' . $product->get_sale_price() .'Lei</span></div>' .'</a>';
echo '<div class="trophies-container__trophy-action">';
echo sprintf(' <a href="?add-to-cart=%d data-quantity="1" class="button product_type_simple add_to_cart_button ajax_add_to_cart" data-product_id="%d" data-product_sku="" rel="nofollow"><i class="fas fa-shopping-cart"></i>Adaugă în coș</a>',$id,$id);
echo '</div>';
echo '</li>';
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
I don't know why i can't take the products by category name. I get no product found. The code look like this
<ul class="glide__slides">
<?php
$args = array(
'post_type' => 'product',
'category_name' => 'trofee',
'posts_per_page' => 20,
'meta_query' => array(
'relation' => 'OR',
array( // Simple products type
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array( // Variable products type
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$id = $product->get_id();
echo '<li class="glide__slide">';
echo '<br /><a href="'.get_permalink().'">' . woocommerce_get_product_thumbnail().' <br> '.get_the_title(). '<br>' . '<div class="bf-sale"><span>' . $product->get_regular_price() .' Lei </span>' . '<span>' . $product->get_sale_price() .'Lei</span></div>' .'</a>';
echo '<div class="trophies-container__trophy-action">';
echo sprintf(' <a href="?add-to-cart=%d data-quantity="1" class="button product_type_simple add_to_cart_button ajax_add_to_cart" data-product_id="%d" data-product_sku="" rel="nofollow"><i class="fas fa-shopping-cart"></i>Adaugă în coș</a>',$id,$id);
echo '</div>';
echo '</li>';
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
Share
Improve this question
asked Nov 8, 2021 at 7:51
Digital PointDigital Point
153 bronze badges
1 Answer
Reset to default 0category_name
is for category taxonomy, sense products use the product_cat
you will need to add a tax_query
into $args
.
Like this.
$args = array(
'post_type' => 'product',
'posts_per_page' => 20,
'tax_query' => [
'taxonomy' => 'product_cat',
'field' => 'name',
'terms' => 'trofee',
],
'meta_query' => array(
'relation' => 'OR',
array( // Simple products type
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array( // Variable products type
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
)
);
本文标签: wp queryTake all produts by category
版权声明:本文标题:wp query - Take all produts by category 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741230805a2362074.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论