编程语言
首页 > 编程语言> > php-WordPress WooCommerce显示单个产品的嵌套产品类别

php-WordPress WooCommerce显示单个产品的嵌套产品类别

作者:互联网

我有一个Wordpress WooCommerce网站,销售汽车零件.对于每个零件(产品),我创建了可以分配给零件的唯一产品类别.所以例如前大灯(零件)可以来自3门1999 Blue Alfa Romeo 156 1.1汽油手册.

在单个产品页面上,我只想显示与该零件关联的产品类别的嵌套列表.因此,当我标记零件时,将有一个嵌套视图,如下图所示.

但是,第二幅图像下方的当前代码显示了所有与之相关的产品类别,包括THIS部分.从下面的第二张图片中可以看出,我将许多其他零件分配给其他汽车品牌,它们全部显示为该零件.我只希望该零件显示与此零件关联的产品类别.因此,在“制作”下仅应显示Alfa Romeo-而不显示其中包含零件的所有其他产品类别,无论它们是否在此零件中进行了标记.

谁能帮忙吗?

当前代码

<?php 
    $woocCategoryTerms = get_terms('product_cat', array(
        'order'        => 'ASC',
        'hide_empty'   => true,  // (boolean) 
        'parent'       => 0,     // (integer) Get direct children of this term (only terms whose explicit parent is this value). If 0 is passed, only top-level terms are returned. Default is an empty string.
        'hierarchical' => true,  // (boolean) Whether to include terms that have non-empty descendants (even if 'hide_empty' is set to true).
        ));    

    foreach($woocCategoryTerms as $wooCategoryTerm) : 
?>
        <ul>
            <li>
                <a href="<?php echo get_term_link( $wooCategoryTerm -> slug, $wooCategoryTerm -> taxonomy ); ?>">
                    <?php 
                        echo $wooCategoryTerm -> name; 
                    ?>
                </a>
                <ul class="wsubcategs">
                    <?php
                        $wooSubArgs = array(
                            'hierarchical' => true,
                            'hide_empty' => true,
                            'parent' => $wooCategoryTerm -> term_id,
                            'taxonomy' => 'product_cat'
                        );

                        $wooSubCategories = get_categories($wooSubArgs);

                        foreach ($wooSubCategories as $wooSubCategory):
                    ?>
                            <li>
                                <a href="<?php echo get_term_link( $wooSubCategory -> slug, $wooSubCategory -> taxonomy );?>">
                                    <?php 
                                        echo $wooSubCategory -> name;
                                    ?>
                                </a>
                            </li>
                            <?php
                        endforeach;
                            ?>  
                </ul>
            </li>
        </ul>
        <?php 
    endforeach; 
        ?>

解决方法:

get_terms返回给定特定分类法的所有术语,而不是帖子.您可以在这里选择两个,但我喜欢使用wp_list_categories的灵活性.它不仅适用于构建类别,还适用于自定义分类法

这是来自法典的例子

<?php
$taxonomy = 'category'; //change to your taxonomy name

// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$separator = ', ';

if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {

    $term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
$terms = rtrim( trim(  str_replace( '<br />',  $separator, $terms ) ), $separator );

// display post categories
echo  $terms;
}
?>

您也可以使用get_the_terms

标签:taxonomy,woocommerce,wordpress,php
来源: https://codeday.me/bug/20191029/1957573.html