具有相关分类法的 Woocommerce 自定义循环
我有一个问题我自己无法解决,所以我希望得到帮助。 我正在开发一个 woocommerce 主题,其中每个产品(特定作者)都与自定义分类法相关联。现在我正在尝试创建一个 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 (),似乎一切都正常。有人能帮助我理解我错在哪里吗?
I have a problem that I can't solve by myself, so I hope for a hand.
I am developing a woocommerce theme, in which each product, a specific author, is associated with a custom taxonomy. Now I'm trying to create a while loop, which calls me all the products that have 2 or more related authors. To achieve this I created the following code:
<?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;
?>
However the loop doesn't show me anything. The weird thing I can't figure out is that, by doing a var_dump (), it seems like everything seems to work. Is anyone able to help me understand where I'm wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了一个与我类似的问题,从给出的答案来看,我像这样改变了我的代码,现在一切正常。我附上它,也许它对将来的人有用。
我只是将焦点从 slug 转移到分类 ID。
I found a question similar to mine, from the answer given, I changed my code like this, and now everything works. I am attaching it, maybe it will be useful to someone in the future.
I simply moved the focus from the slug to the taxonomy ID.