Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何仅显示当前产品父级中的第三个产品类别子级?-吴哥商业_Php_Wordpress_Woocommerce - Fatal编程技术网

Php 如何仅显示当前产品父级中的第三个产品类别子级?-吴哥商业

Php 如何仅显示当前产品父级中的第三个产品类别子级?-吴哥商业,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我很想找到这个问题的答案! 我的WooCommerce产品类别树如下所示: 域 -域1 --Cuvee1 --Cuvee2 --Cuvee3 -域2 -域3 葡萄酒 -胭脂 -布兰科 -罗斯 彩页 -Cépage1 -Cépage2 -Cépage3 在我的一个产品页面上,我选择了Domain1、Cuvee1、Rouge和Cépage1 出于设计目的,我希望只显示Cuvee1,而不显示其他产品类别 到目前为止,我尝试了在网站上找到的几个选项,并尝试对它们进行一些调整,但没有得到任何结果: 代码测

我很想找到这个问题的答案! 我的WooCommerce产品类别树如下所示:


-域1
--Cuvee1
--Cuvee2
--Cuvee3
-域2
-域3
葡萄酒
-胭脂
-布兰科
-罗斯
彩页
-Cépage1
-Cépage2
-Cépage3

在我的一个产品页面上,我选择了Domain1、Cuvee1、Rouge和Cépage1 出于设计目的,我希望只显示Cuvee1,而不显示其他产品类别

到目前为止,我尝试了在网站上找到的几个选项,并尝试对它们进行一些调整,但没有得到任何结果:

代码测试1:

<?php
// get all product cats for the current post
$categories = get_the_terms( get_the_ID(), 'product_cat' ); 
// wrapper to hide any errors from top level categories or products without category

if ( $categories && ! is_wp_error( $category ) ) : 
    // loop through each cat
    foreach($categories as $category) :
      // get the children (if any) of the current cat
      $children = get_categories( array ('taxonomy' => 'product_cat', 'parent' => $category->term_id ));

      if ( count($children) == 0 ) {
          // if no children, then echo the category name.
          echo $category->name;
      }
    endforeach;

endif;

<?php
$terms = get_the_terms( $post->ID, 'product_cat' );                    //Get all terms associated with post in woocommerce's taxonomy 'product_cat'
$term_ids = wp_list_pluck($terms,'term_id');                                            //Get an array of their IDs
$parents = array_filter(wp_list_pluck($terms,'parent'));                                       //Get array of parents - 0 is not a parent
$term_ids_not_parents = array_diff($term_ids,  $parents);                                            //Get array of IDs of terms which are not parents.
$terms_not_parents = array_intersect_key($terms, $term_ids_not_parents);                       //Get corresponding term objects.

foreach($terms_not_parents as $term_not_parent)                                            //Extract the name of the category from the array and post it.
echo $term_not_parent->name;
<?php
global $post;
$cuvee_id = get_term_by('slug', 'domaines', 'product_cat');

$terms = get_the_terms($post->ID, 'product_cat');
foreach ($terms as $term) {
    if($term->parent === $cuvee_id->term_id) {
        echo $term->name;
    }
}
?>