[英]Problem with query products in WooCommerce with category argument in WP_Query
I tried to query products by WP_query and it was all fine.我尝试通过 WP_query 查询产品,一切都很好。 But when i tried query it with category argument nothing happenned.但是当我尝试使用类别参数查询它时,什么也没发生。 When i trying add new shortcode with category args it doesn't work too.当我尝试使用类别 args 添加新的短代码时,它也不起作用。 I can't see where the problem is.我看不出问题出在哪里。 I don't think that swiper.js could make a error.我认为 swiper.js 不会出错。 When i enter empty 'category_name' it shows all products.当我输入空的“category_name”时,它会显示所有产品。 Others argument work as they should其他人的论点按他们应该的方式工作
function test_short($attr) {
$content = '';
$content .= "<script src='https://unpkg.com/swiper/swiper-bundle.min.js'></script>";
$sharg = shortcode_atts( array(
'cat' => '',
), $attr );
$args = array(
'post_type' => 'product',
'category_name' => 'gry',
'suppress_filters' => true
);
$wc_query = new WP_Query($args);
$content .= '<div class="swiper slidee">
<div class="swiper-wrapper">';
if($wc_query->have_posts()) {
while($wc_query->have_posts()) {
$content .= '<div class="swiper-slide">';
$wc_query->the_post();
$id = get_the_ID();
$url = wp_get_attachment_image_src(get_post_thumbnail_id($id), 'full')[0];
$content .= '<img class="product_img" src="'.$url.'">';
$title = get_the_title();
$content .= "<div class='title'>".$title."</div>";
$product = wc_get_product( $id );
$content .= number_format(($product->get_price()),2)." zł";
$content .= '
<a href="?add-to-cart='.$id.'" data-quantity="1" class="button product_type_simple add_to_cart_button ajax_add_to_cart" data-product_id="'.$id.'" data-product_sku="" aria-label="Dodaj „'.$title.'” do koszyka" rel="nofollow"><div class="koszyk_ikona"></div><div class="koszyk_tekst">Dodaj do koszyka</div></a>
';
$content .= '</div>';
}
}
$content .= '
</div>
<div class="swiper-button-next"></div>
<div class="swiper-button-prev"></div>
</div';
$content .= "<script>";
$content .= file_get_contents(get_site_url().'/javascript.js');
$content .= "</script>";
$content .= '<script type="text/javascript"> var swiper = new Swiper(".slidee", {
slidesPerView: 3.5,
spaceBetween: 30,
autoplay: {
delay: 3000,
},
speed: 1700,
navigation: {
nextEl: ".swiper-button-next",
prevEl: ".swiper-button-prev"
}
});
</script>';
wp_reset_query();
return $content;
}
add_shortcode("test_short","test_short");
EDIT.编辑。 I discovered that no of my products have category when i check them by get_the_category() How is possible when i added them to category and at products admin panel they all have some of category当我通过 get_the_category() 检查它们时,我发现我的所有产品都没有类别当我将它们添加到类别并且在产品管理面板中它们都有一些类别时怎么可能
$category = array('Clothing', 'Bed');
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'name',
'terms' => $category
)
)
);
$loop = new WP_Query($args);
if ($loop->have_posts()):
while ($loop->have_posts()) : $loop->the_post();
global $product;
echo '<pre>';
print_r($product->get_name());
echo '</pre>';
endwhile;
endif;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.