【问题标题】:Get the main parent product categories when a product has multiple categories当一个产品有多个分类时,获取主要的父产品分类
【发布时间】:2017-08-09 08:08:41
【问题描述】:

在我公司的 Wordpress 网站上工作,该网站最初是由其他人建立的。 Here's a product page 在那里你可以看到我引用的内容...

为我们创建了“皮肤状况”、“皮肤类型”和“方案”部分。我现在正在尝试复制该行为以创建“所见”部分。它们通过 WooCommerce 类别工作 - 在产品页面上,我们有大量类别列表,我们检查对应的类别,然后显示图标。

最终结果在functions.php和style.css中完成。使用的PHP如下:

add_action('woocommerce_product_meta_end', 'skin_condition');

function skin_condition() {

    echo '<div class="skin-icons">';
    echo '<div class="skin-condition-title"><h4>Skin Condition</h4></div>';

    if ( is_product() ){

        $terms = get_the_terms( $post->ID, 'product_cat' );

        foreach ( $terms as $term ){

            $category_name = $term->name;
            $category_thumbnail = get_woocommerce_term_meta($term->term_id, 'thumbnail_id', true);
            $image = wp_get_attachment_url($category_thumbnail); 

            echo '<div class="skin-condition">';  
            echo '<img class="'.$term->slug.'" title="'.$category_name.'" src="'.$image.'">';
            echo '</div>';
        }
    }
    echo '</div>';
}

因此,这最终会提取应用于产品的所有类别的缩略图。然后用 CSS 来隐藏每个部分我们不想要的东西:

.skin-condition img.glycolic, .skin-condition img.citrix, .skin-condition img.replenix, 
.skin-condition img.glycolix, .skin-condition img.srs, .skin-condition img.solvere, 
.skin-condition img.resurfix, .skin-condition img.rebrightalyze, .skin-condition img.benzaderm, 
.skin-condition img.acne-regimen, .skin-condition img.cleansetone, .skin-condition img.hydrate, 
.skin-condition img.prevent, .skin-condition img.protect, .skin-condition img.repair, 
.skin-condition img.antioxidants, .skin-condition img.body-care, .skin-condition img.cleansers, 
.skin-condition img.exfoliators, .skin-condition img.eye, .skin-condition img.glycolic, 
.skin-condition img.kits-systems, .skin-condition img.masques-peels, .skin-condition img.masques-peels-specialty, 
.skin-condition img.moisturizers, .skin-condition img.retinols, .skin-condition img.sunscreens, 
.skin-condition img.toners, .skin-condition img.new, .skin-condition img.dry, 
.skin-condition img.normal, .skin-condition img.oily, .skin-condition img.sensitive, 
.skin-condition img.dermatopix, .skin-condition .gly-sal, .skin-condition .elite, 
.skin-condition .peels-specialty, .skin-condition .glycolix-elite, .skin-condition .best-sellers, 
.skin-condition .replenix-acne-solutions, .skin-condition .press-mentions  {
    display: none;
}

所以我认为必须有一种方法来避免这种 CSS。
我可以在 PHP 的每个部分中只调用一个父类别吗?而不是全部加载并转到“display: none;”?

如果有帮助的话,类别的小视觉:

谢谢!

【问题讨论】:

    标签: php wordpress woocommerce categories product


    【解决方案1】:

    更新:为避免出现白屏,您必须添加 global $post;。或者,您也可以使用 global $product;$product-&gt;id (而不是 $post-&gt;ID。 p>

    要获取父类(主类),必须使用$term-&gt;parent,这样:

    add_action( 'woocommerce_product_meta_end', 'skin_condition' );
    function skin_condition() {
    
        global $post; // or global $product; and $product->id in get_the_terms() function
    
        if ( is_product() ){
    
            echo '<div class="skin-icons">';
            echo '<div class="skin-condition-title"><h4>Skin Condition</h4></div>';
    
            $terms = get_the_terms( $post->ID, 'product_cat' );
    
            // First loop
            foreach ( $terms as $term ){
    
                // We target the child categories to get the parent ones (based on your screen shot)
                if( $term->parent != 0 ){
                    // Storing the parent term objects
                    $parent_terms_ids[] = $term->parent;
                }
            }
    
            // Second loop
            foreach ( $parent_terms_ids as $term_id ){
    
                // get the object term
                $parent_term = get_term( $term_id, 'product_cat' );
    
                // Only Main parent terms
                if( $parent_term->parent == 0 ){
    
                    $category_thumbnail = get_woocommerce_term_meta( $term_id, 'thumbnail_id', true );
                    $term_image = wp_get_attachment_url($category_thumbnail);
    
                    echo '<div class="skin-condition">';
                    echo '<img class="'.$parent_term->slug.'" title="'.$parent_term->name.'" src="'.$term_image.'">';
                    echo '</div>';
                }
            }
            echo '</div>';
        }
    }
    

    代码位于您的活动子主题(或主题)的 function.php 文件中或任何插件文件中。

    此代码已经过测试并且可以工作。

    【讨论】:

    • 非常感谢您的及时回复!我现在正在尝试实现这一点,但不幸的是,当我尝试清理文件中所有无关的空白时,我的 Wordpress 正在向我抛出死机白屏。 This is my complete functions.php 在尝试清理它之后,但我得到了 WSOD,尽管我没有看到任何语法错误。
    • @Olivia 我已经测试并更新了代码……一开始只是少了一点:global $post;
    • 再次感谢您的帮助!周末我无法访问服务器,所以现在回到它。更新了代码,幸好没有白屏,但它仍然不能正常工作。每个部分都会出现,但图标不会出现,as you can see hereUpdated functions.php file here.
    • 我希望你能再看一眼 :) 但与此同时,我不得不恢复到我们原来的 functions.php 以保持外观。如果您返回,here 是我在之前的评论中使用的最新函数的结果。php,here 是它的源代码的样子。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-16
    • 2015-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多