Wooccommerce自定义循环与相关分类法



我有一个自己无法解决的问题,所以我希望能帮忙。我正在开发一个wooccommerce主题,其中每个产品,一个特定的作者,都与一个自定义的分类法相关联。现在我正在尝试创建一个while循环,它调用所有有2个或更多相关作者的产品。为了实现这一点,我创建了以下代码:

<?php 
$terms = $terms = get_terms([
'taxonomy' => 'autore',
'hide_empty' => true,
]);
$termsArray = array();
foreach( $terms as $term) {
$slug = ' "'.$term -> slug . '", ';
array_push( $termsArray, $slug );
}
$authorSlug = implode( $termsArray);
var_dump($authorSlug);

$custom_loop = new WP_Query( array(
'post_type'      => 'product',
'posts_per_page'=> -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'tax_query' => array(
array( 'taxonomy' => 'autore',
'field'    => 'slug',
'terms'    => array($authorSlug),
)
)
));
if ($custom_loop->have_posts()) : while($custom_loop->have_posts()) : $custom_loop->the_post(); 
the_post_thumbnail('thumbnail', array('class' => '','alt' => get_the_title()));
the_title();
wp_reset_postdata(); 
endwhile; endif;
?>

然而,循环并没有显示任何内容。奇怪的是,我不明白,通过执行var_dump((,似乎一切都正常。有人能帮我明白我错在哪里吗?

我发现了一个与我类似的问题,从给出的答案来看,我这样修改了代码,现在一切都正常了。我正在附上它,也许它将来会对某人有用。

<?php 
$terms = $terms = get_terms([
'taxonomy' => 'autore',
'hide_empty' => true,
]);
$termsArray = array();
foreach( $terms as $term) {
$ids =  $term -> term_id . ' ';
array_push( $termsArray, $ids );
}
$authorID = implode( $termsArray);
var_dump($authorID);

$custom_loop = new WP_Query( array(
'post_type'      => 'product',
'posts_per_page'=> -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'tax_query' => array(
array( 'taxonomy' => 'autore',
'field'    => 'term_id',
'terms'    => $authorID,
'include_children' => true,
'operator' => 'IN',
)
)
));
if ($custom_loop->have_posts()) : while($custom_loop->have_posts()) : $custom_loop->the_post(); 
the_post_thumbnail('thumbnail', array('class' => '','alt' => get_the_title()));
the_title();
wp_reset_postdata(); 
endwhile; endif;
?>

我只是把焦点从slug移到了分类ID上。

最新更新