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条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab