doufeng2877
2016-01-27 16:50
浏览 36
已采纳

循环通过帖子成员类别,检查x的后代(按名称)

I'm using Wordpress with WooCommerce, on the single product page I want to check if one of the product member categories is a member of a parent 'Brands' and if it is display the description from that brand member category.

I've done the following, however it outputs all the categories the product is a member of;

<?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 'Product' is member of 'Brandx' which is a member of 'Brands' then output the description from the 'Brandx' category.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

  • duanjiu2701 2016-01-27 20:34
    最佳回答
       <?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              }
                      }
                     }
                    } ?> 
    
    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题