【问题标题】:Get product category (Woocommerce) with the taxonomy field (ACF) to filter the loop使用分类字段 (ACF) 获取产品类别 (Woocommerce) 以过滤循环
【发布时间】:2021-08-27 06:04:53
【问题描述】:

目前,我正在一个网站上工作,我必须在其中显示使用分类字段选择的类别的帖子;我为此使用高级自定义字段。使用“普通”(单个)帖子和自定义帖子类型,它就像一个魅力。展示它是如何工作的:

<?php
    // get the current taxonomy term
    $term = get_queried_object();

    $catact = get_field('actueel_category', $term);

    $loop = new WP_Query( array(
        'post_type' => 'actueel',
        'posts_per_page' => 2,
        'category__in' => $catact,
      )
    );
    ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

      <a href="<?php the_permalink();?>">

        <div class="post">

          <?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>
          <div class="thumbnail" style="background-image:url('<?php echo $thumb['0'];?>');">
          </div>

          <div class="theme__inner__content">
            <h4><?php the_title();?></h4>
            <span class="more">lees meer</span>
          </div>

        </div>

      </a>

  <?php endwhile; wp_reset_query(); ?>

现在,当我尝试对 Woocommerce 产品做同样的事情时,它不起作用。这是我使用的代码:

<?php
  // get the current taxonomy term
  $term = get_queried_object();

  $catpro = get_field('product_category', $term);

    $loop = new WP_Query( array(
        'post_type' => 'product',
        'posts_per_page' => 2,
        'product_cat' => $catpro,
      )
    );
    ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

      <a href="<?php the_permalink();?>">

        <div class="post">

          <?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>
          <div class="thumbnail" style="background-image:url('<?php echo $thumb['0'];?>');">
          </div>

          <div class="theme__inner__content">
            <h4><?php the_title();?></h4>
            <span class="more">lees meer</span>
          </div>

        </div>

      </a>

  <?php endwhile; wp_reset_query(); ?>

有什么我没有得到的吗?

在管理区域:我使用分类字段,两个输出都显示为术语 ID。对于常规帖子,我选择了“类别”分类法,为产品选择了“product_cat”分类法。

有人可以在这里和我一起思考吗?我似乎无法解决它。也许我忽略了一些东西。

提前致谢!

【问题讨论】:

  • 嗨。为什么使用 get_field()?您使用的是高级自定义字段插件吗?
  • @AdrianaHernández 是的!对不起,忘了提这个。编辑了我的帖子。

标签: php wordpress woocommerce acfpro


【解决方案1】:

使用

'product_cat' => $catpro,

在 WP_Query 中是错误的。这种使用分类参数的方法仅适用于类别分类 - 这是来自非常旧的 WordPress 版本的遗留支持。这就是为什么对于 WP_Query 中的非类别分类法,您需要使用 tax_query。

f.e.

 $loop = new WP_Query( array(
        'post_type' => 'product',
        'posts_per_page' => 2,
        'tax_query' => array(
               array(
                 'taxonomy' => 'product_cat',
                 'field'    => 'term_id',
                 'terms'    => $catpro,
               ),
             ),
           )
         );

但不要忘记调整 FIELD 和 TERMS 值。由于我不知道您的 $catpro 变量是什么,我只是将代码作为示例编写。 字段可以有 term_id、slug、name 值。

更多示例,请查看https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters

【讨论】:

    【解决方案2】:

    您在 WP_Query 中使用product_cat 参数。不支持。

    在产品循环参数中使用category__in

    $loop = new WP_Query( array(
        'post_type' => 'product',
        'posts_per_page' => 2,
        'category__in' => $catpro,
      )
    );
    

    【讨论】:

      猜你喜欢
      • 2019-04-25
      • 1970-01-01
      • 2018-12-19
      • 2018-09-06
      • 2016-01-09
      • 2014-06-27
      • 1970-01-01
      • 2019-07-20
      • 2021-06-23
      相关资源
      最近更新 更多