具有相关分类法的 Woocommerce 自定义循环

发布于 2025-01-13 15:22:45 字数 1119 浏览 0 评论 0原文

我有一个问题我自己无法解决,所以我希望得到帮助。 我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乱世争霸 2025-01-20 15:22:45

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

<?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。

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.

<?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;

?>

I simply moved the focus from the slug to the taxonomy ID.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文