這是關於WooCommerce單一產品頁面。我試圖使用產品類別來顯示相關產品。我可以用下面的代碼來顯示它。使用這將包括當前的文章,它只顯示產品。單一產品頁面 - 從相關產品中排除當前產品
<?php
global $post;
$terms = get_the_terms($post->ID, 'product_cat');
foreach ($terms as $term ) {
$product_cat_name = $term->name;
break;
}
$ids = array();
$currentID = get_the_ID();
$args = array('post_type' => 'product', 'product_cat' => $product_cat_name);
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post(); global $product;
$ids[] = $loop->post->ID;
endwhile;
wp_reset_query();
print_r($ids);
?>
但我試圖阻止當前產品在相關產品上顯示。我嘗試使用下面的代碼的第一秒,但不排除它,它檢索所有默認帖子。
<?php
global $post;
$terms = get_the_terms($post->ID, 'product_cat');
foreach ($terms as $term ) {
$product_cat_name = $term->name;
break;
}
$ids = array();
$currentID = get_the_ID();
$args = array('post_type' => 'product', 'product_cat' => $product_cat_name, 'post__not_in' => array($currentID));
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post(); global $product;
$ids[] = $loop->post->ID;
endwhile;
wp_reset_query();
print_r($ids);
?>
我該如何做到這一點?
感謝
好吧,我有一個功能爲你解答...它應該工作。我在我的代碼中刪除了全局$ product;在這裏不需要。請嘗試並告訴我。 – LoicTheAztec