循环浏览帖子成员类别,检查 x 是否终止(按名称)


Loop through post member categories, check if desendant of x (by name)

我正在使用Wordpress和WooCommerce,在单个产品页面上,我想检查其中一个产品成员类别是否是父"品牌"的成员,以及它是否显示该品牌成员类别的描述。

我已经完成了以下操作,但是它输出了产品所属的所有类别;

<?php $terms = get_the_terms( $post->ID, 'product_cat' );
if($terms){
foreach($terms as $term){
$category_parent_id = $term->parent;
$category_parent = get_category($category_parent_id);
$category_description = $term->description;
//if(($category_parent = "Brands")){ // THIS DOESNT WORK OUTPUTS ALL CATS
if( "Brands" ==  $category_parent ){ // THIS DOESNT WORK EITHER, OUTPUTS NO RESULTS
$category_name = $term->name;
$category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_url($category_thumbnail); ?>
Do Some HTML Stuff Here
<?php }
}
} ?>

Brands >
Brands > Brand1 >
Brands > Brand1 > Product
Brands > Brand2 >
Brands > Brand2 > Product
Brands > Brand9999 >
Brands > Brand9999 > Product
如果"产品"是"品牌"成员的"品牌"

的成员,则输出"品牌"类别的描述。

嗨,

你只是有一个错字

if(($category_parent == "Brands")){ // THIS DOESNT WORK

这就是为什么最好更改一个位置以将基元放在左侧

的原因
 if( "Brands" ==  $category_parent ){ }
   <?php    $terms = get_the_terms( $post->ID, 'product_cat' );
            if($terms){ 
             foreach($terms as $term){
              $category_name = $term->name;
              $brand_lookup = get_term_by('name', 'Brands', 'product_cat');
              $brand_lookup_id = $brand_lookup->term_id;
              $brand_args = array('hierarchical' => 1,'show_option_none' => '','hide_empty' => 0,'parent' => $brand_lookup_id,'taxonomy' => 'product_cat');
              $brands = get_categories($brand_args);
              foreach ($brands as $brand){
               if($category_name==$brand->name){
                $category_description = $term->description;
                $category_slug = get_term_link( $term );
                $category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
                $image = wp_get_attachment_url($category_thumbnail);
                echo $category_name; ?>
<h2>Some HTML funk here</h2>

<?php              }
                  }
                 }
                } ?> 

你应该使用"get_term"而不是"get_category"。

$category_parent = get_term($category_parent_id, 'product_cat');

这将返回术语对象,因此您的"if"需要如下所示:

if ($category_parent->name === 'Brand) {
...