如何使用高级自定义字段(ACF)选择字段过滤/查询Woocommerce类别

时间:2019-02-10 23:31:20

标签: wordpress loops foreach woocommerce advanced-custom-fields

上下文/问题

我正在寻找一种基于ACF选择字段在Woocommerce中遍历子类别的方法。本质上,Woocommerce类别将根据管理员选择的ACF字段分组在一起。 这里的https://imgur.com/a/OkKQZTu是向您显示类别菜单下的ACF字段的示例。

基本上,我希望结果显示如下:

基于ACF选择类别选择A的类别组:

  • Woocommerce子类别3
  • Woocommerce子类别5
  • Woocommerce子类别6

基于ACF选择字段选择B的类别组:

  • Woocommerce儿童类别1
  • Woocommerce子类别4

基于ACF选择字段选择C的类别组:

  • Woocommerce子类别2

代码:

到目前为止,这是我的代码。有了这段代码,我就能获得子类别的第一级及其显示的子类别。但是,再次,我不希望它们按其父类别分组。相反,我希望将它们按ACF选择字段选择进行分组。

<?php 

  $args = array(
          'taxonomy' => 'product_cat',
          'hide_empty' => false,
          'parent'   => 2342,
      );
  $product_cat = get_terms( $args );

  foreach ($product_cat as $parent_product_cat)
  {

  echo '
      <ul>
        <li>
        <a href="'.get_term_link($parent_product_cat->term_id).'">'.$parent_product_cat->name.'</a>
        <ul>
          ';
          $child_args = array(
              'taxonomy'    => 'product_cat',
              'hide_empty'  => false,
              'parent'      => $parent_product_cat->term_id,
          );
  $child_product_cats = get_terms( $child_args );
  foreach ($child_product_cats as $child_product_cat)
  {
    echo '<li>-<a href="'.get_term_link($child_product_cat->term_id).'">'.$child_product_cat->name.'</a></li>';
  }

  echo '</ul>
      </li>
    </ul>';
  }

 ?>

通过使用以下代码,我已经能够确认所选的ACF字段显示的是正确的结果:

<?php 
$cat = get_field('associated_collection', 'category_2414'); 
echo '<p>'.($cat).'</p>'
?>

在这里,asssociated_collection是我的ACF字段,category_2414是父级Woocommerce类别。该代码显示了已选择的正确ACF字段。

关于我可能会缺少的东西或如何实现此目标的任何想法?

在此先感谢您的帮助。让我知道是否需要更清楚地排除故障。

更新

我能够使用此代码显示类别,但我仍然想找到一种方法,根据选择的ACF字段(从“选择字段”选项中)将这些类别组合在一起。使用此代码,它显示为:

ACF字段A

  • Woocommerce类别名称A

ACF字段B

  • Woocommerce类别名称B

ACF字段A

  • Woocommerce类别名称C

我希望它看起来像这样:

ACF字段A

  • Woocommerce类别名称A
  • Woocommerce类别名称C

ACF字段B

  • Woocommerce类别名称B
<?php 

  $args = array(
    'taxonomy' => 'product_cat',
    'hide_empty' => 0
  );
  $c = get_categories($args);
  $c_keep = array();
  foreach($c as  $cat){
    if (get_field('associated_collection', 'category_'.$cat->term_id)) {
      $c_keep[] = $cat; // forgot [] here
    }
  }

  foreach($c_keep as $cat){
    echo '<h1>'.get_field('associated_collection', 'category_'.$cat->term_id).'</h1>';
    echo '<p>'.$cat->name.'</p>';
  }

?>

有什么想法吗?

更新2

我已经修改了一些代码,现在尝试通过该数组循环的另一种方法:

<?php 

function cat_loop() {

global $post;
global $wbdb;

  $args = array(
    'taxonomy' => 'product_cat',
    'hide_empty' => 0
  );

  $c = get_categories($args);
  $c_keep = array();

  foreach($c as $cat){
    if (get_field('associated_collection', 'category_'.$cat->term_id)) {
      $c_keep[] = $cat;
    }
  }

  foreach( $c_keep as $cat ) {
    $subcat_args = array(
      'post_type' => 'product',
      'tax_query' => array(
        array(
          'taxonomy' => 'associated_collection',
          'field'    => 'slug',
          'terms'    => $cat,
        ),
      ),
    );

    print_r($c_keep);

    $loop = null;

    $loop = new WP_Query( $cat_args );

    if ( $loop->have_posts() ) {

      $count = 0;

      while ( $loop->have_posts() ) {

        $loop->the_post();

        // Echo content here

        $count++;

      }

      wp_reset_query();

    } else echo 'Loop finished';

  }

}

cat_loop();

?>

如您在上面所看到的(上面写着echo content here的地方,当循环准备就绪时,我被困在做什么/回声上。

print_r($c_keep)正在向我显示此内容:

Array ( [0] => WP_Term Object ( [term_id] => 2698 [name] => Patio Furniture Table Collections [slug] => patio-furniture-table-collections-category [term_group] => 0 [term_taxonomy_id] => 2698 [taxonomy] => product_cat [description] => [parent] => 2616 [count] => 320 [filter] => raw [meta_value] => 0 [cat_ID] => 2698 [category_count] => 320 [category_description] => [cat_name] => Patio Furniture Table Collections [category_nicename] => patio-furniture-table-collections-category [category_parent] => 2616 ) [1] => WP_Term Object ( [term_id] => 3165 [name] => Sol Teak Patio Furniture [slug] => sol-teak-patio-furniture-category [term_group] => 0 [term_taxonomy_id] => 3165 [taxonomy] => product_cat [description] => [parent] => 2616 [count] => 18 [filter] => raw [meta_value] => 0 [cat_ID] => 3165 [category_count] => 18 [category_description] => [cat_name] => Sol Teak Patio Furniture [category_nicename] => sol-teak-patio-furniture-category [category_parent] => 2616 ) [2] => WP_Term Object ( [term_id] => 2712 [name] => Wicker Patio Furniture (Dining Sets) [slug] => wicker-patio-furniture-dining-sets-category [term_group] => 0 [term_taxonomy_id] => 2712 [taxonomy] => product_cat [description] => [parent] => 2616 [count] => 22 [filter] => raw [meta_value] => 0 [cat_ID] => 2712 [category_count] => 22 [category_description] => [cat_name] => Wicker Patio Furniture (Dining Sets) [category_nicename] => wicker-patio-furniture-dining-sets-category [category_parent] => 2616 ) ) Loop finishedArray ( [0] => WP_Term Object ( [term_id] => 2698 [name] => Patio Furniture Table Collections [slug] => patio-furniture-table-collections-category [term_group] => 0 [term_taxonomy_id] => 2698 [taxonomy] => product_cat [description] => [parent] => 2616 [count] => 320 [filter] => raw [meta_value] => 0 [cat_ID] => 2698 [category_count] => 320 [category_description] => [cat_name] => Patio Furniture Table Collections [category_nicename] => patio-furniture-table-collections-category [category_parent] => 2616 ) [1] => WP_Term Object ( [term_id] => 3165 [name] => Sol Teak Patio Furniture [slug] => sol-teak-patio-furniture-category [term_group] => 0 [term_taxonomy_id] => 3165 [taxonomy] => product_cat [description] => [parent] => 2616 [count] => 18 [filter] => raw [meta_value] => 0 [cat_ID] => 3165 [category_count] => 18 [category_description] => [cat_name] => Sol Teak Patio Furniture [category_nicename] => sol-teak-patio-furniture-category [category_parent] => 2616 ) [2] => WP_Term Object ( [term_id] => 2712 [name] => Wicker Patio Furniture (Dining Sets) [slug] => wicker-patio-furniture-dining-sets-category [term_group] => 0 [term_taxonomy_id] => 2712 [taxonomy] => product_cat [description] => [parent] => 2616 [count] => 22 [filter] => raw [meta_value] => 0 [cat_ID] => 2712 [category_count] => 22 [category_description] => [cat_name] => Wicker Patio Furniture (Dining Sets) [category_nicename] => wicker-patio-furniture-dining-sets-category [category_parent] => 2616 ) ) Loop finishedArray ( [0] => WP_Term Object ( [term_id] => 2698 [name] => Patio Furniture Table Collections [slug] => patio-furniture-table-collections-category [term_group] => 0 [term_taxonomy_id] => 2698 [taxonomy] => product_cat [description] => [parent] => 2616 [count] => 320 [filter] => raw [meta_value] => 0 [cat_ID] => 2698 [category_count] => 320 [category_description] => [cat_name] => Patio Furniture Table Collections [category_nicename] => patio-furniture-table-collections-category [category_parent] => 2616 ) [1] => WP_Term Object ( [term_id] => 3165 [name] => Sol Teak Patio Furniture [slug] => sol-teak-patio-furniture-category [term_group] => 0 [term_taxonomy_id] => 3165 [taxonomy] => product_cat [description] => [parent] => 2616 [count] => 18 [filter] => raw [meta_value] => 0 [cat_ID] => 3165 [category_count] => 18 [category_description] => [cat_name] => Sol Teak Patio Furniture [category_nicename] => sol-teak-patio-furniture-category [category_parent] => 2616 ) [2] => WP_Term Object ( [term_id] => 2712 [name] => Wicker Patio Furniture (Dining Sets) [slug] => wicker-patio-furniture-dining-sets-category [term_group] => 0 [term_taxonomy_id] => 2712 [taxonomy] => product_cat [description] => [parent] => 2616 [count] => 22 [filter] => raw [meta_value] => 0 [cat_ID] => 2712 [category_count] => 22 [category_description] => [cat_name] => Wicker Patio Furniture (Dining Sets) [category_nicename] => wicker-patio-furniture-dining-sets-category [category_parent] => 2616 ) ) Loop finished

关于如何在此循环中回显内容的任何想法?

0 个答案:

没有答案